-
Notifications
You must be signed in to change notification settings - Fork 0
/
register_test.go
45 lines (38 loc) · 1011 Bytes
/
register_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
package main
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestRegisterHandler(t *testing.T) {
// Create service with test configs
s := &Service{
StorageDirectory: "test_config",
TokenSigningKey: []byte("test_config"),
Users: map[string]*User{},
}
// Create a request
requestJSON := `{"username": "testusername", "password": "test_password"}`
reader := strings.NewReader(requestJSON)
req, err := http.NewRequest("POST", "/register", reader)
if err != nil {
t.Fatal(err)
}
// Create ResponseRecorder
rr := httptest.NewRecorder()
handler := http.HandlerFunc(s.registerHandler)
// Call handler
handler.ServeHTTP(rr, req)
// Check the status code
if status := rr.Code; status != 204 {
t.Errorf("handler returned wrong status code: got %v want %v",
status, 204)
}
// Check the response body
expected := ``
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
}