-
Notifications
You must be signed in to change notification settings - Fork 0
/
files_test.go
51 lines (43 loc) · 1.12 KB
/
files_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
package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/context"
)
func TestFilesHandler(t *testing.T) {
mockStore := &MockStorer{}
// Create service with test configs
s := &Service{
StorageDirectory: "./test_files/",
TokenSigningKey: []byte("test_config"),
Store: mockStore,
Users: map[string]*User{
"testusername": {
Username: "testusername",
Password: "test_password",
Folder: "",
},
},
}
testFolderPath := s.StorageDirectory + s.Users["testusername"].Folder
mockStore.On("listFiles", testFolderPath).Return(nil, nil)
// Create a request
req, err := http.NewRequest("POST", "/files", nil)
if err != nil {
t.Fatal(err)
}
// Set context
testclaims := map[string]interface{}{"username": "testusername"}
context.Set(req, "decoded", testclaims)
// Create ResponseRecorder
rr := httptest.NewRecorder()
handler := http.HandlerFunc(s.filesListHandler)
// Call handler
handler.ServeHTTP(rr, req)
// Check the status code
if status := rr.Code; status != 200 {
t.Errorf("handler returned wrong status code: got %v want %v",
status, 200)
}
}