forked from microservices-demo/payment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
component_test.go
51 lines (41 loc) · 1.05 KB
/
component_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
package payment
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/opentracing/opentracing-go"
"golang.org/x/net/context"
)
func TestComponent(t *testing.T) {
// Mechanical stuff.
ctx := context.Background()
handler, logger := WireUp(ctx, float32(99.99), opentracing.GlobalTracer(), "test")
ts := httptest.NewServer(handler)
defer ts.Close()
var request AuthoriseRequest
request.Amount = 9.99
requestBytes, err := json.Marshal(request)
if err != nil {
t.Fatal("ERROR", err)
}
res, err := http.Post(ts.URL+"/paymentAuth", "application/json", bytes.NewReader(requestBytes))
if err != nil {
t.Fatal("ERROR", err)
}
greeting, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal("ERROR", err)
}
var response Authorisation
json.Unmarshal(greeting, &response)
logger.Log("Authorised", response.Authorised)
expected := true
if response.Authorised != expected {
t.Errorf("Authorise returned unexpected result: got %v expected %v",
response.Authorised, expected)
}
}