forked from qvest-digital/loginsrv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
50 lines (39 loc) · 1.14 KB
/
main_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
package main
import (
"github.com/dgrijalva/jwt-go"
. "github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"os"
"strings"
"testing"
"time"
)
func Test_BasicEndToEnd(t *testing.T) {
originalArgs := os.Args
secret := "theSecret"
os.Args = []string{"loginsrv", "-jwt-secret", secret, "-host=localhost", "-port=3000", "-backend=provider=simple,bob=secret"}
defer func() { os.Args = originalArgs }()
go main()
time.Sleep(time.Second)
// success
req, err := http.NewRequest("POST", "http://localhost:3000/login", strings.NewReader(`{"username": "bob", "password": "secret"}`))
NoError(t, err)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/jwt")
r, err := http.DefaultClient.Do(req)
NoError(t, err)
Equal(t, 200, r.StatusCode)
Equal(t, r.Header.Get("Content-Type"), "application/jwt")
b, err := ioutil.ReadAll(r.Body)
NoError(t, err)
token, err := jwt.Parse(string(b), func(*jwt.Token) (interface{}, error) {
return []byte(secret), nil
})
NoError(t, err)
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
Equal(t, "bob", claims["sub"])
} else {
t.Fail()
}
}