-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit_test.go
122 lines (101 loc) · 3.14 KB
/
unit_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
package main
import (
"encoding/json"
"github.com/gorilla/mux"
"net/http"
"net/http/httptest"
"regexp"
"testing"
)
/*
const localProjectURLroot = "http://localhost:8080/"
const localProjectURLbase = "http://localhost:8080/paragliding/"
const localProjectURLinfo1 = "http://localhost:8080/paragliding/api"
const localProjectURLinfo2 = "http://localhost:8080/paragliding/api/"
const localProjectURLarray1 = "http://localhost:8080/paragliding/api/track/"
const localProjectURLarray2 = "http://localhost:8080/paragliding/api/track"
const validIgcURL1 = "http://skypolaris.org/wp-content/uploads/IGS%20Files/Madrid%20to%20Jerez.igc"
const validIgcURL2 = "https://raw.githubusercontent.com/marni/goigc/master/testdata/optimize-long-flight-1.igc"
const contentType = "application/json"
*/
func setup(t *testing.T, method, url string, handler func(w http.ResponseWriter, r *http.Request)) *httptest.ResponseRecorder {
// set up http test
request, err := http.NewRequest(method, url, nil)
if err != nil {
t.Error("unable to create http new request")
}
recorder := httptest.NewRecorder()
router := mux.NewRouter()
router.HandleFunc(url, handler)
router.ServeHTTP(recorder, request)
return recorder
}
// test the root of the URL: "/"
func Test_all(t *testing.T) {
recorder := setup(t, "GET", "/", all)
// checks the http status code
expectedStatusCode := http.StatusNotFound
if recorder.Code != expectedStatusCode {
t.Error(
"incorrect status code received, expected: " +
string(expectedStatusCode) +
" got: " +
string(recorder.Code))
}
}
// tests the redirected URL: "/paragliding"
func Test_redirect(t *testing.T) {
recorder := setup(t, "GET", "/paragliding", rederect)
// checks the http status code
expectedStatusCode := http.StatusPermanentRedirect
if recorder.Code != expectedStatusCode {
t.Error(
"incorrect status code received, expected: " +
string(expectedStatusCode) +
" got: " +
string(recorder.Code))
}
}
// Test_igcinfoapi check if the responding json is as expected
func Test_igcinfoapi(t *testing.T) {
recorder := setup(t, "GET", "/paragliding/api", api)
res := recorder.Body.Bytes()
if res == nil {
t.Error("error reading body")
}
var appInfo GetIgcinfoAPI
// check if values are correct
err3 := json.Unmarshal(res, &appInfo)
if err3 != nil {
t.Error("error umarshaling ", err3)
}
// check http status code
expectedStatusCode := http.StatusOK
if recorder.Code != expectedStatusCode {
t.Error(
"incorrect status code received, expected: " +
string(expectedStatusCode) +
" got: " +
string(recorder.Code))
}
// check if values is correct
expectedVersion := unavalabeVersinNr
if appInfo.Version != expectedVersion {
t.Error("error invalid version nr ")
}
// check if values is correct
expectedInfo := infoSting
if appInfo.Info != expectedInfo {
t.Error("error invalid information string ")
}
// check if format is correct
ok, err4 := regexp.Match(
"P[0-9]*Y?[0-9]*M?[0-9]*W?[0-9]*D?T[0-9]*H?[0-9]*M?[0-9]*S",
[]byte(appInfo.Uptime))
if err4 != nil {
t.Error("error unable to run regex check")
}
if !ok {
t.Error("error incorrect uptime value/format", appInfo.Uptime)
}
}