-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
53 lines (42 loc) · 1.01 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"github.com/julienschmidt/httprouter"
)
func other(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
id := ps.ByName("id")
nid := ps.ByName("nid")
fmt.Fprintf(w, "id: %v and nid: %v\n", id, nid)
}
func headers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
id := ps.ByName("id")
for name, headers := range r.Header {
for _, h := range headers {
fmt.Fprintf(w, "%v: %v\n", name, h)
}
}
fmt.Fprintf(w, "id: %v\n", id)
}
func main() {
mux := httprouter.New()
mux.GET("/headers/:id", headers)
mux.GET("/other/:id/endpoint/:nid", other)
ts := httptest.NewServer(mux)
defer ts.Close()
url := fmt.Sprintf("%s/other/7b2a774e-6d59-11ea-90c6-1f5c859414bc/endpoint/100?q=mytest", ts.URL)
fmt.Println(url)
res, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
greeting, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", greeting)
}