This repository has been archived by the owner on Dec 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb_test.go
112 lines (88 loc) · 2.62 KB
/
web_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
package main
import (
"bytes"
"encoding/json"
ctph "github.com/joekir/ssdeepviz/src/ctph"
"net/http"
"net/http/httptest"
"testing"
)
func TestNewHash_withValidLengthField_ReturnsSerializedFHStruct(t *testing.T) {
var jsonStr = []byte(`{"data_length": 15}`)
req, err := http.NewRequest("POST", "/NewHash", bytes.NewBuffer(jsonStr))
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(NewHash)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusCreated {
t.Errorf("handler returned wrong status code: got %v want %v\n",
status, http.StatusCreated)
}
if ctype := rr.Header().Get("Content-Type"); ctype != "application/json" {
t.Errorf("handler returned wrong content type: got %v want %v\n", ctype, "application/json")
}
t.Logf("%#v\n", rr.Body.String())
}
func TestStepHash_noSession_Returns500(t *testing.T) {
var jsonStr = []byte(`{"byte": 103}`)
req, err := http.NewRequest("POST", "/StepHash", bytes.NewBuffer(jsonStr))
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(StepHash)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusPreconditionRequired {
t.Errorf("handler returned wrong status code: got %v want %v\n",
status, http.StatusPreconditionRequired)
}
}
func TestStepHash_withSession_StepsItByOne(t *testing.T) {
var jsonStr = []byte(`{"data_length": 10}`)
req, err := http.NewRequest("POST", "/NewHash", bytes.NewBuffer(jsonStr))
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(NewHash)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusCreated {
t.Errorf("handler returned wrong status code: got %v want %v\n",
status, http.StatusCreated)
}
cookies := rr.Result().Cookies()
var ctphCookie *http.Cookie
for i := range cookies {
if cookies[i].Name == CTPH_COOKIE {
ctphCookie = cookies[i]
}
}
if nil == ctphCookie {
t.Error("First call did not return a cookie")
}
jsonStr = []byte(`{"byte": 103}`)
req, err = http.NewRequest("POST", "/StepHash", bytes.NewBuffer(jsonStr))
if err != nil {
t.Fatal(err)
}
rr = httptest.NewRecorder()
req.AddCookie(ctphCookie)
handler = http.HandlerFunc(StepHash)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v\n",
status, http.StatusOK)
}
decoder := json.NewDecoder(rr.Body)
var fh ctph.FuzzyHash
err = decoder.Decode(&fh)
if err != nil {
t.Fatal(err)
}
t.Logf("%#v\n", fh)
if fh.Rh.Window[0] != 103 {
t.Fatalf("expected 103, got %d", fh.Rh.Window[0])
}
}