New function GetReason to get HTTP reason phrase from an http.Request
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetReason(r *http.Response) string {
|
||||
if i := strings.IndexByte(r.Status, ' '); i != -1 {
|
||||
return r.Status[i+1:]
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
nethttp "net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEmptyStringIsEmptyReason(t *testing.T) {
|
||||
r := &nethttp.Response{Status: ""}
|
||||
if GetReason(r) != "" {
|
||||
t.Errorf("We consider an empty status line to have an empty reason")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOnlyCodeIsEmptyReason(t *testing.T) {
|
||||
r := &nethttp.Response{Status: "200"}
|
||||
if GetReason(r) != "" {
|
||||
t.Errorf("We consider an status line with just the status code to have an empty reason")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOnlyCodePlusSpaceIsEmptyReason(t *testing.T) {
|
||||
r := &nethttp.Response{Status: "200 "}
|
||||
if GetReason(r) != "" {
|
||||
t.Errorf("We consider an status line with just the status code to have an empty reason")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOneWordReason(t *testing.T) {
|
||||
r := &nethttp.Response{Status: "200 FOO"}
|
||||
if GetReason(r) != "FOO" {
|
||||
t.Errorf("Unexpected reason found")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultiWordReason(t *testing.T) {
|
||||
r := &nethttp.Response{Status: "200 FOO BAR BAZ"}
|
||||
if GetReason(r) != "FOO BAR BAZ" {
|
||||
t.Errorf("Unexpected reason found")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOddSizeStatusCode(t *testing.T) {
|
||||
r := &nethttp.Response{Status: "2 FOO BAR BAZ"}
|
||||
if GetReason(r) != "FOO BAR BAZ" {
|
||||
t.Errorf("Unexpected reason found")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user