diff --git a/bhttp.go b/bhttp.go index 7c53b99..f5085e1 100644 --- a/bhttp.go +++ b/bhttp.go @@ -7,6 +7,7 @@ import ( "io" "io/ioutil" "net/http" + "net/textproto" "net/url" "strings" ) @@ -556,7 +557,7 @@ func UnmarshalBinaryResponse(data []byte) (*http.Response, error) { } headerMap := make(map[string][]string) for _, field := range headerFields.fields { - headerMap[field.name] = []string{field.value} + headerMap[textproto.CanonicalMIMEHeaderKey(field.name)] = []string{field.value} } // Content diff --git a/bhttp_test.go b/bhttp_test.go index f21d2c9..ea905be 100644 --- a/bhttp_test.go +++ b/bhttp_test.go @@ -43,11 +43,25 @@ func TestSimpleResponse(t *testing.T) { StatusCode: 200, Body: ioutil.NopCloser(bytes.NewBufferString("test")), } + testResponse.Header = make(http.Header) + testResponse.Header.Set("Content-Type", "application/json; charset=UTF-8") + testResponse.Header.Set("Host", "example.com") + testResponse.Header.Set("Foo", "Bar") + binaryResponse := CreateBinaryResponse(testResponse) - _, err := binaryResponse.Marshal() + encodedResponse, err := binaryResponse.Marshal() if err != nil { t.Fatal(err) } + + recoveredResponse, err := UnmarshalBinaryResponse(encodedResponse) + require.Nil(t, err, "Unmarshal failed") + + require.Equal(t, binaryResponse.StatusCode, recoveredResponse.StatusCode, "StatusCode mismatch") + require.Equal(t, recoveredResponse.Header.Get("Foo"), "Bar", "Foo header mismatch") + require.Equal(t, recoveredResponse.Header.Get("Host"), "example.com", "Host header mismatch") + require.Equal(t, recoveredResponse.Header.Get("Content-Type"), "application/json; charset=UTF-8", "Content-Type header mismatch") + } func createFullRequestFromParts(method string, url string, headers map[string]string, trailers map[string]string, body []byte) *http.Request {