This repository was archived by the owner on Mar 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest_test.go
More file actions
137 lines (110 loc) · 3.51 KB
/
request_test.go
File metadata and controls
137 lines (110 loc) · 3.51 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package qiwi
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
)
func TestNew(t *testing.T) {
trans := New("billId", "SiteID", "TOKEN", "")
dummy := &Payment{}
if reflect.TypeOf(trans) != reflect.TypeOf(dummy) {
t.Errorf("New() wrong return %T must be %T type", reflect.TypeOf(trans), reflect.TypeOf(dummy))
}
}
func TestNewRequest(t *testing.T) {
req, _ := newRequest(context.TODO(), "POST", "http://example.com/Init", "TOKEN", nil)
dummy := &http.Request{}
if reflect.TypeOf(req) != reflect.TypeOf(dummy) {
t.Errorf("newRequest() wrong return %T must be %T type", reflect.TypeOf(req), reflect.TypeOf(dummy))
}
}
func TestRequestHeaders(t *testing.T) {
link, _ := url.Parse("http://example.com/Init")
req, _ := newRequest(context.TODO(), "POST", link.String(), "TOKEN", nil)
if req.URL.Hostname() != link.Hostname() {
t.Error("Wrong hostname")
}
if req.Header.Get("Content-Type") != "application/json" {
t.Error("Wrong content-type")
}
if req.Header.Get("Accept") != "application/json" {
t.Error("Wrong Accept")
}
if req.Header.Get("Authorization") != "Bearer TOKEN" {
t.Error("No authorization token")
}
}
func TestProceedBadJSONRequest(t *testing.T) {
// ErrBadJSON
serv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "{{{{bad json")
}))
serv.Start()
defer serv.Close()
// Route request to mocked http server
payload := New("billId", "SiteID", "TOKEN", serv.URL)
err := proceedRequest(context.Background(), "POST", "/Init", payload)
if !errors.Is(err, ErrBadJSON) {
t.Errorf("Wrong error for bad JSON return: %s", err)
}
}
func TestProceedBadHTTPStatusRequest(t *testing.T) {
// ErrBadStatusReply
serv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Something wrong", http.StatusInternalServerError)
}))
serv.Start()
defer serv.Close()
// Route request to mocked http server
payload := New("billId", "SiteID", "TOKEN", serv.URL)
err := proceedRequest(context.TODO(), "POST", "/Init", payload)
if !errors.Is(err, ErrBadStatusReply) {
t.Errorf("Wrong error for error HTTP error code response: %s", err)
}
}
func TestProceeRequestWithError(t *testing.T) {
// ErrReply
serv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `{
"serviceName":"payin-core",
"errorCode":"validation.error",
"description":"Validation error",
"userMessage":"Validation error",
"dateTime":"2018-11-13T16:49:59.166+03:00",
"traceId":"fd0e2a08c63ace83"
}`)
}))
serv.Start()
defer serv.Close()
// Route request to mocked http server
payload := New("billId", "SiteID", "TOKEN", serv.URL)
err := proceedRequest(context.TODO(), "PUT", "/Init", payload)
if !errors.Is(err, ErrReplyWithError) {
t.Errorf("Remote error not parsed: %s", err)
}
if payload.Description != "Validation error" {
t.Error("Wrong RSP error message")
}
if payload.ErrCode != "validation.error" {
t.Error("Wrong RSP error code")
}
}
func TestProceedRequest(t *testing.T) {
// GoodRequest
serv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reply := `{}`
fmt.Fprintln(w, reply)
}))
defer serv.Close()
// Route request to mocked http server
payload := New("billId", "SiteID", "TOKEN", serv.URL)
err := proceedRequest(context.TODO(), "POST", "/Init", payload)
if err != nil {
t.Errorf("Error shoud be empty: %s", err)
}
}