MCPcopy Create free account
hub / github.com/gavv/httpexpect / TestResponse_Text

Function TestResponse_Text

response_test.go:929–991  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

927}
928
929func TestResponse_Text(t *testing.T) {
930 t.Run("basic", func(t *testing.T) {
931 reporter := newMockReporter(t)
932
933 headers := map[string][]string{
934 "Content-Type": {"text/plain; charset=utf-8"},
935 }
936
937 body := `hello, world!`
938
939 httpResp := &http.Response{
940 StatusCode: http.StatusOK,
941 Header: http.Header(headers),
942 Body: io.NopCloser(bytes.NewBufferString(body)),
943 }
944
945 resp := NewResponse(reporter, httpResp)
946 assert.Equal(t, body, resp.Body().Raw())
947 resp.chain.assert(t, success)
948
949 respText := resp.Text()
950 respText.chain.assert(t, success)
951 resp.chain.assert(t, success)
952 assert.Equal(t, "hello, world!", respText.Raw())
953
954 NewResponse(reporter, httpResp).HasContentType("text/plain").
955 chain.assert(t, success)
956
957 NewResponse(reporter, httpResp).HasContentType("text/plain", "utf-8").
958 chain.assert(t, success)
959
960 NewResponse(reporter, httpResp).HasContentType("application/json").
961 chain.assert(t, failure)
962
963 NewResponse(reporter, httpResp).
964 chain.assert(t, success)
965 })
966
967 t.Run("read failure", func(t *testing.T) {
968 reporter := newMockReporter(t)
969
970 headers := map[string][]string{
971 "Content-Type": {"text/plain; charset=utf-8"},
972 }
973
974 body := newMockBody(`hello, world!`)
975 body.readErr = errors.New("read error")
976
977 httpResp := &http.Response{
978 StatusCode: http.StatusOK,
979 Header: http.Header(headers),
980 Body: body,
981 }
982
983 resp := NewResponse(reporter, httpResp)
984 resp.chain.assert(t, success)
985
986 respText := resp.Text()

Callers

nothing calls this directly

Calls 10

BodyMethod · 0.95
TextMethod · 0.95
newMockReporterFunction · 0.85
NewResponseFunction · 0.85
newMockBodyFunction · 0.85
HeaderMethod · 0.80
assertMethod · 0.80
HasContentTypeMethod · 0.80
EqualMethod · 0.45
RawMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…