-
Notifications
You must be signed in to change notification settings - Fork 0
/
documenthandlerjson_test.go
85 lines (64 loc) · 1.77 KB
/
documenthandlerjson_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
package main
import (
"fmt"
"io/ioutil"
"mime"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func initJSON(filePath string) *httptest.Server {
file := FileDescriptor{}
doc := DocumentDescriptor{}
handle := ResponseHandle{}
file.New(filePath)
doc.New(file)
handle.New(doc)
ts := httptest.NewServer(http.HandlerFunc(handle.Handle))
return ts
}
func TestGetJSONHandle(t *testing.T) {
ts := initJSON("testdata/food-mexican(url).json")
defer ts.Close()
res, _ := http.Get(ts.URL + "/food/mexican/mole")
greeting, _ := ioutil.ReadAll(res.Body)
fmt.Printf("%s", greeting)
}
func TestPostJSONHandle(t *testing.T) {
ts := initJSON("testdata/food-mexican(url).json")
defer ts.Close()
body := strings.NewReader(`{
"description": "This is the piquito de Gallo",
"id": 4,
"title": "Piquito de Gallo very tasty",
"url": "gallote"
}`)
res, _ := http.Post(ts.URL+"/food/mexican/", mime.TypeByExtension(".json"), body)
greeting, _ := ioutil.ReadAll(res.Body)
fmt.Printf("%s", greeting)
}
func TestPutJSONHandle(t *testing.T) {
ts := initJSON("testdata/food-mexican(url).json")
defer ts.Close()
body := strings.NewReader(`{
"description": "This is the piquito de Gallo",
"id": 4,
"title": "Piquito de Gallo very tasty",
"url": "gallote"
}`)
client := &http.Client{}
req, _ := http.NewRequest(http.MethodPut, ts.URL+"/food/mexican/mole", body)
res, _ := client.Do(req)
greeting, _ := ioutil.ReadAll(res.Body)
fmt.Printf("%s", greeting)
}
func TestDeleteJSONHandle(t *testing.T) {
ts := initJSON("testdata/food-mexican(url).json")
defer ts.Close()
client := &http.Client{}
req, _ := http.NewRequest(http.MethodDelete, ts.URL+"/food/mexican/mole", nil)
res, _ := client.Do(req)
greeting, _ := ioutil.ReadAll(res.Body)
fmt.Printf("%s", greeting)
}