-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.go
100 lines (73 loc) · 1.89 KB
/
webserver.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
package testhelper
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
)
type Test struct {
Hello string `json:"hello"`
}
type RawBody struct {
RawBody string `json:"raw-body"`
}
type Header struct {
ContentType string `json:"content_type"`
}
func setUpRoutes() *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
var test Test
if err := json.NewDecoder(r.Body).Decode(&test); err != nil {
panic(err)
}
w.WriteHeader(200)
if err := json.NewEncoder(w).Encode(test); err != nil {
panic(err)
}
}).Methods("POST")
r.HandleFunc("/reflect-header", func(w http.ResponseWriter, r *http.Request) {
header := Header{
ContentType: r.Header.Get("Content-Type"),
}
w.WriteHeader(200)
if err := json.NewEncoder(w).Encode(header); err != nil {
panic(err)
}
}).Methods("POST")
r.HandleFunc("/send-cookie", func(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("cookiemonster")
if err != nil {
panic(err)
}
w.WriteHeader(200)
if err := json.NewEncoder(w).Encode(cookie); err != nil {
panic(err)
}
}).Methods("POST")
r.HandleFunc("/raw-body", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
if err := json.NewEncoder(w).Encode(&RawBody{RawBody: "test value"}); err != nil {
panic(err)
}
}).Methods("GET")
r.HandleFunc("/get-cookie", func(w http.ResponseWriter, r *http.Request) {
cookie := &http.Cookie{
Name: "cookiemonster",
Value: "cookiemonster",
Path: "/",
Secure: false,
HttpOnly: false,
Domain: "localhost",
}
cookie.Unparsed = nil
http.SetCookie(w, cookie)
w.WriteHeader(200)
if err := json.NewEncoder(w).Encode(cookie); err != nil {
panic(err)
}
}).Methods("POST")
r.HandleFunc("/empty-body", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}).Methods("GET")
return r
}