mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-02 13:44:16 +09:00
test: consolidate Go tests (#39962)
Consolidate all Go tests. Coverage is identical to before. Go tests are around 5% faster while being 7% smaller. No non-test code is touched.
This commit is contained in:
1 parent
ce7364e215
commit
8abda62312
388 files changed
+18410
-29561
No files matched your search
@@ -28,43 +28,31 @@ func TestRouteMock(t *testing.T) {
|
||||
}
|
||||
r.Get("/foo", middleware1, RouterMockPoint("mock-point"), middleware2, handler)
|
||||
|
||||
// normal request
|
||||
recorder := httptest.NewRecorder()
|
||||
req, err := http.NewRequest(http.MethodGet, "http://localhost:8000/foo", nil)
|
||||
assert.NoError(t, err)
|
||||
r.ServeHTTP(recorder, req)
|
||||
assert.Len(t, recorder.Header(), 3)
|
||||
assert.Equal(t, "m1", recorder.Header().Get("X-Test-Middleware1"))
|
||||
assert.Equal(t, "m2", recorder.Header().Get("X-Test-Middleware2"))
|
||||
assert.Equal(t, "h", recorder.Header().Get("X-Test-Handler"))
|
||||
RouteMockReset()
|
||||
|
||||
// mock at "mock-point"
|
||||
RouteMock("mock-point", func(resp http.ResponseWriter, req *http.Request) {
|
||||
resp.Header().Set("X-Test-MockPoint", "a")
|
||||
resp.WriteHeader(http.StatusOK)
|
||||
})
|
||||
recorder = httptest.NewRecorder()
|
||||
req, err = http.NewRequest(http.MethodGet, "http://localhost:8000/foo", nil)
|
||||
assert.NoError(t, err)
|
||||
r.ServeHTTP(recorder, req)
|
||||
assert.Len(t, recorder.Header(), 2)
|
||||
assert.Equal(t, "m1", recorder.Header().Get("X-Test-Middleware1"))
|
||||
assert.Equal(t, "a", recorder.Header().Get("X-Test-MockPoint"))
|
||||
RouteMockReset()
|
||||
|
||||
// mock at MockAfterMiddlewares
|
||||
RouteMock(MockAfterMiddlewares, func(resp http.ResponseWriter, req *http.Request) {
|
||||
resp.Header().Set("X-Test-MockPoint", "b")
|
||||
resp.WriteHeader(http.StatusOK)
|
||||
})
|
||||
recorder = httptest.NewRecorder()
|
||||
req, err = http.NewRequest(http.MethodGet, "http://localhost:8000/foo", nil)
|
||||
assert.NoError(t, err)
|
||||
r.ServeHTTP(recorder, req)
|
||||
assert.Len(t, recorder.Header(), 3)
|
||||
assert.Equal(t, "m1", recorder.Header().Get("X-Test-Middleware1"))
|
||||
assert.Equal(t, "m2", recorder.Header().Get("X-Test-Middleware2"))
|
||||
assert.Equal(t, "b", recorder.Header().Get("X-Test-MockPoint"))
|
||||
RouteMockReset()
|
||||
for _, tc := range []struct {
|
||||
mockPoint, mockValue string
|
||||
headers [][2]string
|
||||
}{
|
||||
// normal request
|
||||
{headers: [][2]string{{"X-Test-Middleware1", "m1"}, {"X-Test-Middleware2", "m2"}, {"X-Test-Handler", "h"}}},
|
||||
// mock at "mock-point"
|
||||
{mockPoint: "mock-point", mockValue: "a", headers: [][2]string{{"X-Test-Middleware1", "m1"}, {"X-Test-MockPoint", "a"}}},
|
||||
// mock at MockAfterMiddlewares
|
||||
{mockPoint: MockAfterMiddlewares, mockValue: "b", headers: [][2]string{{"X-Test-Middleware1", "m1"}, {"X-Test-Middleware2", "m2"}, {"X-Test-MockPoint", "b"}}},
|
||||
} {
|
||||
if tc.mockPoint != "" {
|
||||
RouteMock(tc.mockPoint, func(resp http.ResponseWriter, req *http.Request) {
|
||||
resp.Header().Set("X-Test-MockPoint", tc.mockValue)
|
||||
resp.WriteHeader(http.StatusOK)
|
||||
})
|
||||
}
|
||||
recorder := httptest.NewRecorder()
|
||||
req, err := http.NewRequest(http.MethodGet, "http://localhost:8000/foo", nil)
|
||||
assert.NoError(t, err)
|
||||
r.ServeHTTP(recorder, req)
|
||||
assert.Len(t, recorder.Header(), len(tc.headers))
|
||||
for _, header := range tc.headers {
|
||||
assert.Equal(t, header[1], recorder.Header().Get(header[0]))
|
||||
}
|
||||
RouteMockReset()
|
||||
}
|
||||
}
|
||||
+12
-24
@@ -208,30 +208,18 @@ func TestRouter(t *testing.T) {
|
||||
method: "GET",
|
||||
handlerMarks: []string{"not-found:/api/v1"},
|
||||
})
|
||||
testRoute(t, "GET /api/v1/repos/the-user/the-repo/branches", resultStruct{
|
||||
method: "GET",
|
||||
pathParams: map[string]string{"username": "the-user", "reponame": "the-repo"},
|
||||
})
|
||||
|
||||
testRoute(t, "POST /api/v1/repos/the-user/the-repo/branches", resultStruct{
|
||||
method: "POST",
|
||||
pathParams: map[string]string{"username": "the-user", "reponame": "the-repo"},
|
||||
})
|
||||
|
||||
testRoute(t, "GET /api/v1/repos/the-user/the-repo/branches/master", resultStruct{
|
||||
method: "GET",
|
||||
pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "name": "master"},
|
||||
})
|
||||
|
||||
testRoute(t, "PATCH /api/v1/repos/the-user/the-repo/branches/master", resultStruct{
|
||||
method: "PATCH",
|
||||
pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "name": "master"},
|
||||
})
|
||||
|
||||
testRoute(t, "DELETE /api/v1/repos/the-user/the-repo/branches/master", resultStruct{
|
||||
method: "DELETE",
|
||||
pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "name": "master"},
|
||||
})
|
||||
for _, method := range []string{"GET", "POST"} {
|
||||
testRoute(t, method+" /api/v1/repos/the-user/the-repo/branches", resultStruct{
|
||||
method: method,
|
||||
pathParams: map[string]string{"username": "the-user", "reponame": "the-repo"},
|
||||
})
|
||||
}
|
||||
for _, method := range []string{"GET", "PATCH", "DELETE"} {
|
||||
testRoute(t, method+" /api/v1/repos/the-user/the-repo/branches/master", resultStruct{
|
||||
method: method,
|
||||
pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "name": "master"},
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("MatchPath", func(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user