This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapi_test.go
156 lines (135 loc) · 3.78 KB
/
api_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package pi
import (
"fmt"
"io/ioutil"
"os"
"testing"
)
func TestGenerateRequestApiBaseEnvExist(t *testing.T) {
// prepare
beforeEnv := os.Getenv("PIXELA_API_BASE")
afterEnv := "pixela.example.com"
os.Setenv("PIXELA_API_BASE", afterEnv)
testToken := "thisissecret"
testUsername := "c-know"
testAgreement := "false"
testMinor := "true"
testParamStruct := &createUserParams{
Token: testToken,
Username: testUsername,
AgreeTerms: testAgreement,
NotMinor: testMinor,
}
// test call
testMethod := "POST"
testPath := "v1/users/c-know"
req, err := generateRequest(testMethod, testPath, testParamStruct)
// cleanup
os.Setenv("PIXELA_API_BASE", beforeEnv)
// assertion
if err != nil {
t.Errorf("Unexpected error occurs. %s", err)
}
if req.Method != testMethod {
t.Errorf("Unexpected request method. %s", req.Method)
}
if req.URL.String() != fmt.Sprintf("https://%s/%s", afterEnv, testPath) {
t.Errorf("Unexpected request path. %s", req.URL.String())
}
b, err := ioutil.ReadAll(req.Body)
defer req.Body.Close()
if err != nil {
t.Errorf("Failed to read request body. %s", err)
}
if string(b) != fmt.Sprintf("{\"token\":\"%s\",\"username\":\"%s\",\"agreeTermsOfService\":\"%s\",\"notMinor\":\"%s\",\"thanksCode\":\"\"}", testToken, testUsername, testAgreement, testMinor) {
t.Errorf("Unexpected request body. %s", string(b))
}
if req.Header.Get("Content-Type") != "application/json" {
t.Errorf("Unexpected request header. %s", req.Header.Get("Content-Type"))
}
}
func TestGenerateRequestApiBaseEnvNotExist(t *testing.T) {
// prepare
beforeEnv := os.Getenv("PIXELA_API_BASE")
afterEnv := ""
os.Setenv("PIXELA_API_BASE", afterEnv)
testToken := "thisissecret"
testUsername := "c-know"
testAgreement := "false"
testMinor := "true"
testParamStruct := &createUserParams{
Token: testToken,
Username: testUsername,
AgreeTerms: testAgreement,
NotMinor: testMinor,
}
// test call
testMethod := "POST"
testPath := "v1/users/c-know"
req, err := generateRequest(testMethod, testPath, testParamStruct)
// cleanup
os.Setenv("PIXELA_API_BASE", beforeEnv)
// assertion
if err != nil {
t.Errorf("Failed to generate request. %s", err)
}
if req.URL.String() != fmt.Sprintf("https://pixe.la/%s", testPath) {
t.Errorf("Unexpected request path. %s", req.URL.String())
}
}
func TestGenerateRequestBodyIsNil(t *testing.T) {
// prepare
testMethod := "POST"
testPath := "v1/users/c-know"
// test call
req, err := generateRequest(testMethod, testPath, nil)
// cleanup
// nop
// assertion
if err != nil {
t.Errorf("Unexpected error occurs. %s", err)
}
if req.Body != nil {
b, err := ioutil.ReadAll(req.Body)
defer req.Body.Close()
if err != nil {
t.Errorf("Failed to read request body. %s", err)
}
t.Errorf("Unexpected request body. %s", string(b))
}
}
func TestGenerateRequestWithToken(t *testing.T) {
// prepare
beforeEnv := os.Getenv("PIXELA_USER_TOKEN")
afterEnv := "thisissecret"
os.Setenv("PIXELA_USER_TOKEN", afterEnv)
// test call
testMethod := "POST"
testPath := "v1/users/c-know"
req, err := generateRequestWithToken(testMethod, testPath, nil)
// cleanup
os.Setenv("PIXELA_USER_TOKEN", beforeEnv)
// assertion
if err != nil {
t.Errorf("Unexpected error occurs. %s", err)
}
if req.Header.Get("X-USER-TOKEN") != afterEnv {
t.Errorf("Unexpected request header. %s", req.Header.Get("X-USER-TOKEN"))
}
}
func TestGenerateRequestWithTokenNoToken(t *testing.T) {
// prepare
beforeEnv := os.Getenv("PIXELA_USER_TOKEN")
afterEnv := ""
os.Setenv("PIXELA_USER_TOKEN", afterEnv)
// test call
testMethod := "POST"
testPath := "v1/users/c-know"
_, err := generateRequestWithToken(testMethod, testPath, nil)
// cleanup
os.Setenv("PIXELA_USER_TOKEN", beforeEnv)
// assertion
if err == nil {
t.Errorf("Error should has occurs.")
}
}