-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend_selector_test.go
99 lines (94 loc) · 2.06 KB
/
backend_selector_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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"testing"
)
func TestBackendSelector(t *testing.T) {
bs := BackendSelector{
Mapping: map[string]string{
"mjolnir-slots": "abc:8080",
},
}
checkedBodies := 0
for i, tc := range []struct {
host string
body string
expectedHost string
expectedErr error
}{
{
host: "example.com",
body: sampleSessionBody,
expectedHost: "abc:8080",
expectedErr: nil,
},
{
host: "example.com",
body: "{}",
expectedHost: "example.com",
expectedErr: Err404,
},
{
host: "example.com",
body: `{"game":"unknown"}`,
expectedHost: "example.com",
expectedErr: Err404,
},
} {
req, err := http.NewRequest("POST", fmt.Sprintf("http://%s/path", tc.host), ioutil.NopCloser(bytes.NewReader([]byte(tc.body))))
if err != nil {
t.Errorf("%d: %s", i, err.Error())
}
err = bs.ModifyRequest(req)
if err != tc.expectedErr {
t.Errorf("%d: unexpected error: %s", i, err.Error())
}
if tc.expectedHost != req.URL.Host {
t.Errorf("%d: unexpected host: %s", i, req.URL.Host)
}
if err == nil {
b, err := ioutil.ReadAll(req.Body)
if err != nil {
t.Error(err)
return
}
if string(b) != string(tc.body) {
t.Errorf("unexpected request body: %s", string(b))
return
}
checkedBodies++
}
}
if checkedBodies == 0 {
t.Errorf("the test did not check a single final request body")
}
}
var sampleSessionBody = `{
"casino_id": "s4",
"game": "mjolnir-slots",
"currency": "EUR",
"user":
{
"id": "3422",
"email": "gamma.solutions@example.com",
"firstname": "John",
"lastname": "Doe",
"nickname": "John Doe",
"city": "Bucharest",
"country": "RO",
"date_of_birth": "1983-12-19",
"registered_at": "2017-05-15",
"gender": "m"
},
"locale": "en",
"ip": "195.222.65.88",
"balance": 113875,
"urls":
{
"deposit_url": "http://s4.casino.softswiss.com/accounts/EUR/deposit",
"return_url": "http://s4.casino.softswiss.com/exit_iframe"
}
}`