-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2142 from zhulongcheng/custom-404
fix(http): replace default-404 handler with custom-404 handler
- Loading branch information
Showing
2 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package http | ||
|
||
import ( | ||
"go.uber.org/zap" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestAPIHandler_NotFound(t *testing.T) { | ||
type args struct { | ||
method string | ||
path string | ||
} | ||
type wants struct { | ||
statusCode int | ||
contentType string | ||
body string | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
wants wants | ||
}{ | ||
{ | ||
name: "path not found", | ||
args: args{ | ||
method: "GET", | ||
path: "/404", | ||
}, | ||
wants: wants{ | ||
statusCode: http.StatusNotFound, | ||
contentType: "application/json; charset=utf-8", | ||
body: ` | ||
{ | ||
"code": "not found", | ||
"msg": "path not found" | ||
}`, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
||
r := httptest.NewRequest(tt.args.method, tt.args.path, nil) | ||
w := httptest.NewRecorder() | ||
|
||
b := &APIBackend{} | ||
b.Logger = zap.NewNop() | ||
|
||
h := NewAPIHandler(b) | ||
h.ServeHTTP(w, r) | ||
|
||
res := w.Result() | ||
content := res.Header.Get("Content-Type") | ||
body, _ := ioutil.ReadAll(res.Body) | ||
|
||
if res.StatusCode != tt.wants.statusCode { | ||
t.Errorf("%q. get %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode) | ||
} | ||
if tt.wants.contentType != "" && content != tt.wants.contentType { | ||
t.Errorf("%q. get %v, want %v", tt.name, content, tt.wants.contentType) | ||
} | ||
if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { | ||
t.Errorf("%q. get\n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) | ||
} | ||
|
||
}) | ||
} | ||
} |