|
1 | 1 | package rest
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "github.com/ant0ine/go-json-rest/rest/test" |
5 | 4 | "testing"
|
| 5 | + |
| 6 | + "github.com/ant0ine/go-json-rest/rest/test" |
6 | 7 | )
|
7 | 8 |
|
8 | 9 | func TestRecorderMiddleware(t *testing.T) {
|
@@ -91,3 +92,43 @@ func TestRecorderAndGzipMiddleware(t *testing.T) {
|
91 | 92 | recorded.CodeIs(200)
|
92 | 93 | recorded.ContentTypeIsJson()
|
93 | 94 | }
|
| 95 | + |
| 96 | +//Underlying net/http only allows you to set the status code once |
| 97 | +func TestRecorderMiddlewareReportsSameStatusCodeAsResponse(t *testing.T) { |
| 98 | + api := NewApi() |
| 99 | + const firstCode = 400 |
| 100 | + const secondCode = 500 |
| 101 | + |
| 102 | + // a middleware carrying the Env tests |
| 103 | + api.Use(MiddlewareSimple(func(handler HandlerFunc) HandlerFunc { |
| 104 | + return func(w ResponseWriter, r *Request) { |
| 105 | + |
| 106 | + handler(w, r) |
| 107 | + |
| 108 | + if r.Env["STATUS_CODE"] == nil { |
| 109 | + t.Error("STATUS_CODE is nil") |
| 110 | + } |
| 111 | + statusCode := r.Env["STATUS_CODE"].(int) |
| 112 | + if statusCode != firstCode { |
| 113 | + t.Errorf("STATUS_CODE = %d expected, got %d", firstCode, statusCode) |
| 114 | + } |
| 115 | + } |
| 116 | + })) |
| 117 | + |
| 118 | + // the middleware to test |
| 119 | + api.Use(&RecorderMiddleware{}) |
| 120 | + |
| 121 | + // a simple app |
| 122 | + api.SetApp(AppSimple(func(w ResponseWriter, r *Request) { |
| 123 | + w.WriteHeader(firstCode) |
| 124 | + w.WriteHeader(secondCode) |
| 125 | + })) |
| 126 | + |
| 127 | + // wrap all |
| 128 | + handler := api.MakeHandler() |
| 129 | + |
| 130 | + req := test.MakeSimpleRequest("GET", "http://localhost/", nil) |
| 131 | + recorded := test.RunRequest(t, handler, req) |
| 132 | + recorded.CodeIs(firstCode) |
| 133 | + recorded.ContentTypeIsJson() |
| 134 | +} |
0 commit comments