-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
126 lines (100 loc) · 2.95 KB
/
main_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
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
package main
import (
"testing"
"bytes"
"fmt"
"log"
"reflect"
"runtime"
"strings"
"github.com/EUDAT-GEF/BridgIt/api"
"github.com/EUDAT-GEF/BridgIt/utils"
"encoding/json"
"net/http"
"net/http/httptest"
"github.com/EUDAT-GEF/BridgIt/def"
)
var accessToken = "yJYcu3KjXqawyaeMIKPBuJc1ArCkAGFJIDQwgf89wP5JBOEl"
var inputFile = "http://weblicht.sfs.uni-tuebingen.de/clrs/storage/1507625318314.txt"
var testService = "fake"
var testConfig = "./tests/test_config.json"
func TestClient(t *testing.T) {
config, err := utils.ReadConfigFile(testConfig)
CheckErr(t, err)
app := api.NewApp(config)
go app.Start()
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
handler := http.HandlerFunc(app.Index)
// Our handlers satisfy http.Handler, so we can call their ServeHTTP method
// directly and pass in our Request and ResponseRecorder.
handler.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
// Check the response body is what we expect.
expected := app.Info
var infoReply def.Info
err = json.NewDecoder(rr.Body).Decode(&infoReply)
CheckErr(t, err)
ExpectEquals(t, infoReply, expected)
req, err = http.NewRequest("POST", "/jobs?service="+testService+"&token="+accessToken+"&input="+inputFile, nil)
if err != nil {
t.Fatal(err)
}
rr = httptest.NewRecorder()
handler = http.HandlerFunc(app.JobStart)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
// expectedLink := app.Config.GEFAddress + "/api/volumes/OutputVolume/results.txt?content&access_token=" + accessToken
CheckErr(t, err)
// ExpectEquals(t, rr.Body.String(), expectedLink)
log.Println("Stopping HTTP server")
err = app.Server.Shutdown(nil)
CheckErr(t, err)
}
func CheckErr(t *testing.T, err error) {
if err != nil {
t.Log(err, caller())
t.FailNow()
}
}
func ExpectEquals(t *testing.T, left, right interface{}) {
if !reflect.DeepEqual(left, right) {
t.Logf("Not Equals:\n%#v\n%#v\n@%s", left, right, caller())
t.FailNow()
}
}
func ExpectNotEquals(t *testing.T, left, right interface{}) {
if reflect.DeepEqual(left, right) {
t.Logf("Equals (should not be):\n%#v\n%#v\n@%s", left, right, caller())
t.FailNow()
}
}
func ExpectNotNil(t *testing.T, value interface{}) {
if value == nil {
t.Log("Unexpected NIL value", caller())
t.FailNow()
}
}
func caller() string {
var b bytes.Buffer
for i := 2; i < 5; i++ {
_, file, line, ok := runtime.Caller(i)
if ok &&
!strings.HasSuffix(file, "/src/testing/testing.go") &&
!strings.Contains(file, "/src/runtime/") {
b.WriteString(fmt.Sprint("\n", file, ":", line))
}
}
return b.String()
}