-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlatch_request_test.go
More file actions
120 lines (101 loc) · 4.62 KB
/
latch_request_test.go
File metadata and controls
120 lines (101 loc) · 4.62 KB
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
package golatch
import (
"net/url"
"reflect"
"testing"
"time"
)
//Example request with dummy data used on the following tests
var example_date = time.Date(2015, time.February, 15, 14, 53, 0, 0, time.UTC) //2015-02-15 14:53:00
var example_request = &LatchRequest{AppID: "MyAppID",
SecretKey: "MySecretKey",
HttpMethod: "POST",
URL: GetLatchURL("pair/my_token"),
XHeaders: map[string]string{
"X-11paths-B": "Test value",
"X-11paths-A": "Line\nBreaks",
},
Params: url.Values{
"B": {"B", "A"},
"A": {"A"},
},
Date: example_date,
}
var example_expected_formatted_date = "2015-02-15 14:53:00"
var example_expected_serialized_headers = "x-11paths-a:Line Breaks x-11paths-b:Test value"
var example_expected_serialized_params = "A=A&B=A&B=B"
var example_expected_signature = "POST\n" +
example_expected_formatted_date + "\n" +
example_expected_serialized_headers + "\n" +
"/api/1.0/pair/my_token\n" +
example_expected_serialized_params
var example_expected_header = "11PATHS MyAppID XOxtWhv576pq8w7VAC8EoxN3SPo="
func TestNewLatchRequest(t *testing.T) {
got_request := NewLatchRequest(example_request.AppID, example_request.SecretKey, example_request.HttpMethod, example_request.URL, example_request.XHeaders, example_request.Params, example_date)
if example_request.AppID != got_request.AppID ||
example_request.SecretKey != got_request.SecretKey ||
example_request.HttpMethod != got_request.HttpMethod ||
!reflect.DeepEqual(example_request.URL, got_request.URL) ||
!reflect.DeepEqual(example_request.XHeaders, got_request.XHeaders) ||
!reflect.DeepEqual(example_request.Params, got_request.Params) ||
example_request.Date != got_request.Date {
t.Errorf("NewLatchRequest() failed: expected %q, got %q", example_request, got_request)
}
}
func TestGetAuthenticationHeaders(t *testing.T) {
headers := example_request.GetAuthenticationHeaders()
if headers == nil {
t.Errorf("GetAuthenticationHeaders() failed: returned value should not be nil")
}
if _, ok := headers["Authorization"]; !ok {
t.Errorf("GetAuthenticationHeaders() failed: missing authorization header, got %q", headers)
}
if _, ok := headers["X-11Paths-Date"]; !ok {
t.Errorf("GetAuthenticationHeaders() failed: missing authorization header date, got %q", headers)
}
}
func TestGetRequestSignature(t *testing.T) {
got_signature := example_request.GetRequestSignature()
if example_expected_signature != got_signature {
t.Errorf("GetRequestSignature() failed: expected %q (%x), got %q(%x)", example_expected_signature, example_expected_signature, got_signature, got_signature)
}
}
func TestGetSerializedHeaders(t *testing.T) {
got_headers := example_request.GetSerializedHeaders()
if example_expected_serialized_headers != got_headers {
t.Errorf("GetSerializedHeaders() failed: expected %q, got %q", example_expected_serialized_headers, got_headers)
}
}
func TestGetSerializedParams(t *testing.T) {
got_params := example_request.GetSerializedParams()
if example_expected_serialized_params != got_params {
t.Errorf("GetSerializedParams() failed: expected %q, got %q", example_expected_serialized_params, got_params)
}
}
func TestGetAuthorizationHeader(t *testing.T) {
got_header := example_request.GetAuthorizationHeader()
if example_expected_header != got_header {
t.Errorf("GetAuthorizationHeader() failed: expected %q, got %q", example_expected_header, got_header)
}
}
func TestGetFormattedDate(t *testing.T) {
got_formatted_date := example_request.GetFormattedDate()
if example_expected_formatted_date != got_formatted_date {
t.Errorf("GetFormattedDate() failed: expected %q, got %q", example_expected_formatted_date, got_formatted_date)
}
}
func TestGetHttpRequest(t *testing.T) {
got_request := example_request.GetHttpRequest()
if got_request.Method != example_request.HttpMethod {
t.Errorf("GetHttpRequest() failed: expected HTTP Method %q, got %q", example_request.HttpMethod, got_request.Method)
}
if got_request.URL.String() != "https://latch.elevenpaths.com/api/1.0/pair/my_token" {
t.Errorf("GetHttpRequest() failed: expected URL %q, got %q", "https://latch.elevenpaths.com/api/1.0/pair/my_token", got_request.URL.String())
}
if got_request.Header.Get(API_AUTHORIZATION_HEADER_NAME) != example_request.GetAuthorizationHeader() {
t.Errorf("GetHttpRequest() failed: expected Authorization header %q, got %q", got_request.Header.Get(API_AUTHORIZATION_HEADER_NAME), example_request.GetAuthorizationHeader())
}
if got_request.Header.Get(API_DATE_HEADER_NAME) != example_request.GetFormattedDate() {
t.Errorf("GetHttpRequest() failed: expected Date header %q, got %q", got_request.Header.Get(API_DATE_HEADER_NAME), example_request.GetFormattedDate())
}
}