Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit 215fc93

Browse files
authored
Merge pull request #29 from InVisionApp/improve-status-code-tracking
Handle StatusCode with StopExecution
2 parents 327d676 + a437bde commit 215fc93

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

rye.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,12 @@ func (m *MWHandler) do(w http.ResponseWriter, r *http.Request, handler Handler)
126126
}
127127

128128
// Write the error out
129-
statusCode = strconv.Itoa(resp.StatusCode)
130129
WriteJSONStatus(w, "error", resp.Error(), resp.StatusCode)
131130
}()
131+
132+
if resp.StatusCode > 0 {
133+
statusCode = strconv.Itoa(resp.StatusCode)
134+
}
132135
}
133136

134137
handlerName := getFuncName(handler)

rye_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,16 @@ var _ = Describe("Rye", func() {
185185
})
186186
})
187187

188+
Context("when a handler returns a response with StopExecution and StatusCode", func() {
189+
It("should not execute any further handlers", func() {
190+
h := mwHandler.Handle([]Handler{stopExecutionWithStatusHandler, successHandler})
191+
h.ServeHTTP(response, request)
192+
193+
Eventually(inc).Should(Receive(Equal(statsInc{"handlers.stopExecutionWithStatusHandler.404", 1, float32(STATRATE)})))
194+
Expect(os.Getenv(RYE_TEST_HANDLER_ENV_VAR)).ToNot(Equal("1"))
195+
})
196+
})
197+
188198
Context("when a before handler returns a response with StopExecution", func() {
189199
It("should not execute any further handlers", func() {
190200
request.Method = "OPTIONS"
@@ -330,6 +340,13 @@ func stopExecutionHandler(rw http.ResponseWriter, r *http.Request) *Response {
330340
}
331341
}
332342

343+
func stopExecutionWithStatusHandler(rw http.ResponseWriter, r *http.Request) *Response {
344+
return &Response{
345+
StopExecution: true,
346+
StatusCode: 404,
347+
}
348+
}
349+
333350
func testFunc() {}
334351

335352
func HaveTiming(name string, statrate float32) types.GomegaMatcher {

0 commit comments

Comments
 (0)