github.com/webonyx/up@v0.7.4-0.20180808230834-91b94e551323/internal/proxy/response_test.go (about) 1 package proxy 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/tj/assert" 8 ) 9 10 func Test_JSON_isTextMime(t *testing.T) { 11 assert.Equal(t, isTextMime("application/json"), true) 12 assert.Equal(t, isTextMime("application/json; charset=utf-8"), true) 13 assert.Equal(t, isTextMime("Application/JSON"), true) 14 } 15 16 func Test_XML_isTextMime(t *testing.T) { 17 assert.Equal(t, isTextMime("application/xml"), true) 18 assert.Equal(t, isTextMime("application/xml; charset=utf-8"), true) 19 assert.Equal(t, isTextMime("ApPlicaTion/xMl"), true) 20 } 21 22 func TestResponseWriter_Header(t *testing.T) { 23 w := NewResponse() 24 w.Header().Set("Foo", "bar") 25 w.Header().Set("Bar", "baz") 26 27 var buf bytes.Buffer 28 w.header.Write(&buf) 29 30 assert.Equal(t, "Bar: baz\r\nFoo: bar\r\n", buf.String()) 31 } 32 33 func TestResponseWriter_Write_text(t *testing.T) { 34 types := []string{ 35 "text/x-custom", 36 "text/plain", 37 "text/plain; charset=utf-8", 38 "application/json", 39 "application/json; charset=utf-8", 40 "application/xml", 41 "image/svg+xml", 42 } 43 44 for _, kind := range types { 45 t.Run(kind, func(t *testing.T) { 46 w := NewResponse() 47 w.Header().Set("Content-Type", kind) 48 w.Write([]byte("hello world\n")) 49 50 e := w.End() 51 assert.Equal(t, 200, e.StatusCode) 52 assert.Equal(t, "hello world\n", e.Body) 53 assert.Equal(t, kind, e.Headers["Content-Type"]) 54 assert.False(t, e.IsBase64Encoded) 55 }) 56 } 57 } 58 59 func TestResponseWriter_Write_binary(t *testing.T) { 60 w := NewResponse() 61 w.Header().Set("Content-Type", "image/png") 62 w.Write([]byte("data")) 63 64 e := w.End() 65 assert.Equal(t, 200, e.StatusCode) 66 assert.Equal(t, "ZGF0YQ==", e.Body) 67 assert.Equal(t, "image/png", e.Headers["Content-Type"]) 68 assert.True(t, e.IsBase64Encoded) 69 } 70 71 func TestResponseWriter_Write_gzip(t *testing.T) { 72 w := NewResponse() 73 w.Header().Set("Content-Type", "text/plain") 74 w.Header().Set("Content-Encoding", "gzip") 75 w.Write([]byte("data")) 76 77 e := w.End() 78 assert.Equal(t, 200, e.StatusCode) 79 assert.Equal(t, "ZGF0YQ==", e.Body) 80 assert.Equal(t, "text/plain", e.Headers["Content-Type"]) 81 assert.True(t, e.IsBase64Encoded) 82 } 83 84 func TestResponseWriter_WriteHeader(t *testing.T) { 85 w := NewResponse() 86 w.WriteHeader(404) 87 w.Write([]byte("Not Found\n")) 88 89 e := w.End() 90 assert.Equal(t, 404, e.StatusCode) 91 assert.Equal(t, "Not Found\n", e.Body) 92 assert.Equal(t, "text/plain; charset=utf8", e.Headers["Content-Type"]) 93 }