Skip to content

Commit

Permalink
fallback invalid method to RequestUnmarshaler
Browse files Browse the repository at this point in the history
  • Loading branch information
acoshift committed Aug 31, 2021
1 parent d7dee3a commit f9fc63f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 59 deletions.
16 changes: 0 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,6 @@ Content-Type: application/json; charset=utf-8
}
```

### Method not allowed

- Developer (api caller) send invalid method

```http
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
{
"ok": false,
"error": {
"message": "method not allowed"
}
}
```

### Function not found

- Developer (api caller) call not exists function
Expand Down
57 changes: 28 additions & 29 deletions arpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,40 +75,39 @@ func (m *Manager) encoder(w http.ResponseWriter, r *http.Request, v interface{})
}

func (m *Manager) decoder(r *http.Request, v interface{}) error {
if v, ok := v.(RequestAdapter); ok {
v.AdaptRequest(r)
if p, ok := v.(RequestAdapter); ok {
p.AdaptRequest(r)
}

if r.Method != http.MethodPost {
return errMethodNotAllowed
if r.Method == http.MethodPost {
mt, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type"))
switch mt {
case "application/json":
return WrapError(json.NewDecoder(r.Body).Decode(v))
case "application/x-www-form-urlencoded":
err := r.ParseForm()
if err != nil {
return WrapError(err)
}
if v, ok := v.(FormUnmarshaler); ok {
return WrapError(v.UnmarshalForm(r.PostForm))
}
case "multipart/form-data":
err := r.ParseMultipartForm(32 << 20)
if err != nil {
return WrapError(err)
}
if v, ok := v.(MultipartFormUnmarshaler); ok {
return WrapError(v.UnmarshalMultipartForm(r.MultipartForm))
}
}
}

mt, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type"))
switch mt {
case "application/json":
return WrapError(json.NewDecoder(r.Body).Decode(v))
case "application/x-www-form-urlencoded":
err := r.ParseForm()
if err != nil {
return WrapError(err)
}
if v, ok := v.(FormUnmarshaler); ok {
return WrapError(v.UnmarshalForm(r.PostForm))
}
case "multipart/form-data":
err := r.ParseMultipartForm(32 << 20)
if err != nil {
return WrapError(err)
}
if v, ok := v.(MultipartFormUnmarshaler); ok {
return WrapError(v.UnmarshalMultipartForm(r.MultipartForm))
}
default:
// fallback to request unmarshaler
if v, ok := v.(RequestUnmarshaler); ok {
return WrapError(v.UnmarshalRequest(r))
}
// fallback to request unmarshaler
if v, ok := v.(RequestUnmarshaler); ok {
return WrapError(v.UnmarshalRequest(r))
}

return ErrUnsupported
}

Expand Down
12 changes: 0 additions & 12 deletions arpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,6 @@ func TestInvalidContentType(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, w.Code)
}

func TestInvalidMethod(t *testing.T) {
t.Parallel()

m := arpc.New()
h := m.Handler(sum)
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
h.ServeHTTP(w, r)

assert.Equal(t, http.StatusBadRequest, w.Code)
}

func TestNotFound(t *testing.T) {
t.Parallel()

Expand Down
3 changes: 1 addition & 2 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ var (
)

var (
errMethodNotAllowed = NewProtocolError("method not allowed")
errNotFound = NewProtocolError("not found")
errNotFound = NewProtocolError("not found")
)

type internalError struct{}
Expand Down

0 comments on commit f9fc63f

Please sign in to comment.