-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver_test.go
More file actions
90 lines (83 loc) · 2.13 KB
/
server_test.go
File metadata and controls
90 lines (83 loc) · 2.13 KB
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
package main
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/artemzi/olx-parser/version"
"github.com/gorilla/mux"
)
func TestHealthHandler(t *testing.T) {
r := mux.NewRouter()
r.HandleFunc("/healthz", healthz).Methods("GET")
req, err := http.NewRequest("GET", "/healthz", nil)
if err != nil {
t.Error(err)
}
trw := httptest.NewRecorder()
r.ServeHTTP(trw, req)
if trw.Body.String() != "Ok" {
t.Error("Expected", "Ok", "got", trw.Body.String())
}
if trw.Code != 200 {
t.Error("Expected status:", 200, "got", trw.Body.String())
}
}
func TestInfoHandler(t *testing.T) {
r := mux.NewRouter()
r.HandleFunc("/info", info).Methods("GET")
req, err := http.NewRequest("GET", "/info", nil)
if err != nil {
t.Error(err)
}
trw := httptest.NewRecorder()
r.ServeHTTP(trw, req)
inf := "{\"commit\":\"" + version.COMMIT + "\",\"repo\":\"" + version.REPO +
"\",\"version\":\"" + version.RELEASE + "\"}"
if trw.Body.String() != inf {
t.Error("Expected", inf, "got", trw.Body.String())
}
if trw.Code != 200 {
t.Error("Expected status:", 200, "got", trw.Body.String())
}
}
func TestRootHandler(t *testing.T) {
r := mux.NewRouter()
r.HandleFunc("/", root).Methods("GET")
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Error(err)
}
trw := httptest.NewRecorder()
r.ServeHTTP(trw, req)
inf := fmt.Sprintf("PARSER v%s\n", version.RELEASE)
if trw.Body.String() != inf {
t.Error("Expected", inf, "got", trw.Body.String())
}
if trw.Code != 200 {
t.Error("Expected status:", 200, "got", trw.Body.String())
}
}
func TestNotFoundHandler(t *testing.T) {
r := mux.NewRouter()
r.NotFoundHandler = http.HandlerFunc(logging(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
io.WriteString(w, `Not Found`)
}))
req, err := http.NewRequest("GET",
"/b6c8528975c1af6e14dac4e61",
nil)
if err != nil {
t.Error(err)
}
trw := httptest.NewRecorder()
r.ServeHTTP(trw, req)
msg := "Not Found"
if trw.Body.String() != msg {
t.Error("Expected", msg, "got", trw.Body.String())
}
if trw.Code != 404 {
t.Error("Expected status:", 404, "got", trw.Body.String())
}
}