Skip to content

Commit

Permalink
refactor: send http status 503 for temporary errors
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Adler <[email protected]>
  • Loading branch information
michaeladler committed Jan 9, 2024
1 parent 8ddcc77 commit 7fbd177
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ the cookie would have already timed out.
Based on the plugin's response, wfx can:

- Modify the incoming request before it undergoes further processing by wfx in the usual manner.
- Send a preemptive response back to the client, such as a "permission denied" message.
- Send a preemptive response back to the client, such as a "permission denied" or "service unavailable" message.
- Leave the request unchanged.

### Use Cases
Expand Down
4 changes: 2 additions & 2 deletions example/plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func main() {
Payload: &plugin.PayloadT{
Type: plugin.Payloadgenerated_plugin_client_Response,
Value: &client.ResponseT{
Status: client.ResponseStatusDeny,
Content: []byte("Plugin is overloaded. Please try again later.\n"),
Status: client.ResponseStatusError,
Content: []byte("Plugin overloaded. Please try again later.\n"),
},
},
}
Expand Down
2 changes: 2 additions & 0 deletions fbs/client/response.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ enum ResponseStatus: byte {
Accept = 1,
// The request was modifified and may proceed.
Modified = 2,
// There was a temporary problem processing the request.
Unavailable = 3,
}

// Response contains the response details for a client.
Expand Down
21 changes: 12 additions & 9 deletions generated/plugin/client/ResponseStatus.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions middleware/plugin/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ func (mw *MW) Wrap(next http.Handler) http.Handler {
w.WriteHeader(http.StatusOK)
case client.ResponseStatusDeny:
w.WriteHeader(http.StatusForbidden)

Check warning on line 82 in middleware/plugin/middleware.go

View check run for this annotation

Codecov / codecov/patch

middleware/plugin/middleware.go#L79-L82

Added lines #L79 - L82 were not covered by tests
case client.ResponseStatusUnavailable:
w.WriteHeader(http.StatusServiceUnavailable)
}
_, _ = w.Write(val.Content)
return
Expand Down
41 changes: 41 additions & 0 deletions middleware/plugin/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func TestNewMiddleware_ModifyRequest(t *testing.T) {
},
}
handler.ServeHTTP(recorder, httpReq)
assert.Equal(t, http.StatusOK, recorder.Code)
}

func TestNewMiddleware_SendResponse(t *testing.T) {
Expand Down Expand Up @@ -123,6 +124,46 @@ func TestNewMiddleware_SendResponse(t *testing.T) {
},
}
handler.ServeHTTP(recorder, httpReq)
assert.Equal(t, http.StatusOK, recorder.Code)
}

func TestNewMiddleware_Unavailable(t *testing.T) {
p := NewTestPlugin()
go func() {
for msg := range p.chMessage {
msg.response <- plugin.PluginResponseT{
Cookie: msg.request.Cookie,
Payload: &plugin.PayloadT{
Type: plugin.Payloadgenerated_plugin_client_Response,
Value: &client.ResponseT{
Status: client.ResponseStatusUnavailable,
Envelope: []*client.EnvelopeT{
{Name: "User-Agent", Values: []string{"gotest"}},
},
Content: []byte{},
},
},
}
}
}()

chQuit := make(chan error)
mw, err := NewMiddleware(p, chQuit)
require.Nil(t, err)
t.Cleanup(mw.Shutdown)

handler := mw.Wrap(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {}))

recorder := httptest.NewRecorder()
httpReq := &http.Request{
Method: http.MethodGet,
URL: &url.URL{
Host: "localhost",
Path: "/foo",
},
}
handler.ServeHTTP(recorder, httpReq)
assert.Equal(t, http.StatusServiceUnavailable, recorder.Code)
}

func TestHttpMethodToAction(t *testing.T) {
Expand Down

0 comments on commit 7fbd177

Please sign in to comment.