-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth2-cli_test.go
52 lines (44 loc) · 1.5 KB
/
oauth2-cli_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
package main
import (
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)
func TestCreateRequestClientGrant(t *testing.T) {
host := "https://example.com"
cid := "cid"
cpw := "cpw"
uid := "uid"
upw := "upw"
typ := clientGrant
request, err := createRequest(&host, &cid, &cpw, &uid, &upw, &typ)
assert.NotNil(t, request, "Request was nil")
assertNoError(t, err)
assertAuthorizationHeader(t, request)
assertContentTypeHeader(t, request)
}
func assertNoError(t *testing.T, err int) bool {
return assert.EqualValues(t, 0, err, "Error should be zero")
}
func assertAuthorizationHeader(t *testing.T, request *http.Request) bool {
return assertRequestHeaderValue(t, request, "Authorization", "Basic Y2lkOmNwdw==")
}
func assertContentTypeHeader(t *testing.T, request *http.Request) bool {
return assertRequestHeaderValue(t, request, "Content-Type", "application/x-www-form-urlencoded")
}
func assertRequestHeaderValue(t *testing.T, actual *http.Request, headerName string, headerValueExpected string) bool {
return assert.EqualValues(t, headerValueExpected, actual.Header.Get(headerName), "Header %q not set correctly", headerName)
}
func TestCreateRequestPasswordGrant(t *testing.T) {
host := "https://example.com"
cid := "cid"
cpw := "cpw"
uid := "uid"
upw := "upw"
typ := passwordGrant
request, err := createRequest(&host, &cid, &cpw, &uid, &upw, &typ)
assert.NotNil(t, request, "Request was nil")
assertNoError(t, err)
assertAuthorizationHeader(t, request)
assertContentTypeHeader(t, request)
}