-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_schema_download_test.go
59 lines (50 loc) · 1.43 KB
/
handle_schema_download_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
)
func Test_handleSchemaDownload(t *testing.T) {
tests := map[string]struct {
schemaID string
expectedResult string
expectedCode int
}{
"can download a valid schema": {
schemaID: "config-schema",
expectedResult: `{"mock":"schema"}`,
expectedCode: 200,
},
"can handle schema not found error": {
schemaID: "not-found",
expectedResult: "schema not found\n",
expectedCode: 404,
},
}
for testName, test := range tests {
t.Run(testName, func(t *testing.T) {
db := newMockDbClient()
router := mux.NewRouter()
server := newServer(db, router)
r, _ := http.NewRequest("GET", fmt.Sprintf("/schema/%s", test.schemaID), nil)
w := httptest.NewRecorder()
// As unit testing individual handler directly, without calling .ServeHTTP on the router,
// these tests will need to manually set URL variables on the router as part of test setup.
r = mux.SetURLVars(r, map[string]string{
"id": test.schemaID,
})
handlerUnderTest := server.handleSchemaDownload()
handlerUnderTest(w, r)
var respBody bytes.Buffer
if _, err := respBody.ReadFrom(w.Body); err != nil {
t.Errorf("Could not read body: %v", err)
}
assert.Equal(t, test.expectedCode, w.Code)
assert.Equal(t, test.expectedResult, respBody.String())
})
}
}