goyave.dev/goyave/v4@v4.4.11/native_handler_test.go (about) 1 package goyave 2 3 import ( 4 "context" 5 "io" 6 "net/http" 7 "net/http/httptest" 8 "strings" 9 "testing" 10 ) 11 12 type NativeHandlerTestSuite struct { 13 TestSuite 14 } 15 16 func (suite *NativeHandlerTestSuite) TestNativeHandler() { 17 request := &Request{ 18 httpRequest: httptest.NewRequest("GET", "/native", nil), 19 } 20 recorder := httptest.NewRecorder() 21 response := newResponse(recorder, nil) 22 handler := NativeHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 23 w.Write([]byte("Hello world")) 24 })) 25 26 handler(response, request) 27 result := recorder.Result() 28 body, err := io.ReadAll(result.Body) 29 if err != nil { 30 panic(err) 31 } 32 result.Body.Close() 33 suite.Equal(200, result.StatusCode) 34 suite.Equal("Hello world", string(body)) 35 suite.False(response.empty) 36 } 37 38 func (suite *NativeHandlerTestSuite) TestNativeHandlerBody() { 39 40 handler := NativeHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 41 res, err := io.ReadAll(r.Body) 42 if err != nil { 43 w.WriteHeader(http.StatusInternalServerError) 44 return 45 } 46 47 suite.Equal("request=content", string(res)) 48 w.WriteHeader(http.StatusNoContent) 49 })) 50 51 suite.RunServer(func(router *Router) { 52 router.Route("POST", "/native", handler) 53 }, func() { 54 headers := map[string]string{"Content-Type": "application/x-www-form-urlencoded; param=value"} 55 resp, err := suite.Post("/native", headers, strings.NewReader("request=content")) 56 if err != nil { 57 panic(err) 58 } 59 defer resp.Body.Close() 60 61 suite.Equal(http.StatusNoContent, resp.StatusCode) 62 }) 63 } 64 65 func (suite *NativeHandlerTestSuite) TestNativeHandlerBodyJSON() { 66 67 handler := NativeHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 68 res, err := io.ReadAll(r.Body) 69 if err != nil { 70 w.WriteHeader(http.StatusInternalServerError) 71 return 72 } 73 74 suite.Equal("{\"request\":\"content\"}", string(res)) 75 w.WriteHeader(http.StatusNoContent) 76 })) 77 78 suite.RunServer(func(router *Router) { 79 router.Route("POST", "/native", handler) 80 }, func() { 81 headers := map[string]string{"Content-Type": "application/json"} 82 resp, err := suite.Post("/native", headers, strings.NewReader("{\"request\":\"content\"}")) 83 if err != nil { 84 panic(err) 85 } 86 defer resp.Body.Close() 87 88 suite.Equal(http.StatusNoContent, resp.StatusCode) 89 }) 90 } 91 92 func (suite *NativeHandlerTestSuite) TestNativeMiddleware() { 93 request := &Request{ 94 httpRequest: httptest.NewRequest("GET", "/native", nil), 95 } 96 recorder := httptest.NewRecorder() 97 response := newResponse(recorder, nil) 98 middleware := NativeMiddleware(func(next http.Handler) http.Handler { 99 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 100 w.Write([]byte("Hello world")) 101 next.ServeHTTP(w, r) 102 }) 103 }) 104 105 handlerExecuted := false 106 handler := func(response *Response, r *Request) { 107 result := recorder.Result() 108 body, err := io.ReadAll(result.Body) 109 if err != nil { 110 panic(err) 111 } 112 result.Body.Close() 113 suite.Equal(200, result.StatusCode) 114 suite.Equal("Hello world", string(body)) 115 suite.False(response.empty) 116 handlerExecuted = true 117 } 118 middleware(handler)(response, request) 119 suite.True(handlerExecuted) 120 121 middleware = NativeMiddleware(func(next http.Handler) http.Handler { 122 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 123 // Blocking middleware 124 }) 125 }) 126 127 handlerExecuted = false 128 handler = func(response *Response, r *Request) { 129 handlerExecuted = true 130 } 131 middleware(handler)(response, request) 132 suite.False(handlerExecuted) 133 } 134 135 func (suite *NativeHandlerTestSuite) TestNativeMiddlewareReplacesRequest() { 136 request := &Request{ 137 httpRequest: httptest.NewRequest("GET", "/native", nil), 138 } 139 recorder := httptest.NewRecorder() 140 response := newResponse(recorder, nil) 141 var requestWithContext *http.Request 142 middleware := NativeMiddleware(func(next http.Handler) http.Handler { 143 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 144 type key int 145 ctx := context.WithValue(r.Context(), key(0), "value") 146 requestWithContext = r.WithContext(ctx) 147 next.ServeHTTP(w, requestWithContext) 148 }) 149 }) 150 151 handlerExecuted := false 152 handler := func(response *Response, r *Request) { 153 suite.Same(requestWithContext, r.Request()) 154 handlerExecuted = true 155 } 156 middleware(handler)(response, request) 157 suite.True(handlerExecuted) 158 } 159 160 func TestNativeHandlerTestSuite(t *testing.T) { 161 RunTest(t, new(NativeHandlerTestSuite)) 162 }