This repository has been archived by the owner on Mar 25, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequests_test.go
86 lines (69 loc) · 2.09 KB
/
requests_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
package themap
import (
"context"
"fmt"
// "net"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestNewRequest(t *testing.T) {
req, _ := newRequest(context.Background(), "POST", "/Init", 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 TestProceedRequest(t *testing.T) {
var err error
payload := New("TestTerminal", "TestOrder123")
//listner, _ := net.Listen("tcp", APILink+":8060")
// ErrBadJSON
servBadjson := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "{{{{bad json")
}))
//serv_badjson.Listener = listner
servBadjson.Start()
defer servBadjson.Close()
// Route request to mocked http server
APILink = servBadjson.URL
err = proceedRequest(context.Background(), "POST", "/Init", payload)
if err != ErrBadJSON {
t.Errorf("Wrong error for bad JSON return")
}
// ErrBadStatusReply
servErrcode := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Something wrong", http.StatusInternalServerError)
}))
servErrcode.Start()
defer servErrcode.Close()
// Route request to mocked http server
APILink = servErrcode.URL
err = proceedRequest(context.Background(), "POST", "/Init", payload)
if err != ErrBadStatusReply {
t.Errorf("Wrong error for error HTTP error code response")
}
// GoodRequest
serv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reply := `{
"Success": true,
"OrderId": "TestOrder123",
"Amount": 300,
"ErrCode": "",
"Type": "pay",
"SessionGUID": "1ILZMU42Zs8YivEsYXOA67ijRYs"
}`
fmt.Fprintln(w, reply)
}))
defer serv.Close()
// Route request to mocked http server
APILink = serv.URL
err = proceedRequest(context.Background(), "POST", "/Init", payload)
if payload.SessionGUID != "1ILZMU42Zs8YivEsYXOA67ijRYs" {
t.Errorf("Wrong return in HTTP response")
}
if err != nil {
t.Errorf("Error shoud be empty")
}
}