This repository has been archived by the owner on Mar 20, 2020. It is now read-only.
forked from husobee/vestigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.go
94 lines (79 loc) · 2.87 KB
/
examples_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
package vestigo_test
import (
"fmt"
"net/http"
"net/http/httptest"
"time"
"github.com/husobee/vestigo"
)
func ExampleManyRoutes() {
// new router
router := vestigo.NewRouter()
// standard http.HandlerFunc
handler := func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("version: %s, resource: %s\n", vestigo.Param(r, "version"), r.URL.Path)
}
// setup a GET /v:version/hi endpoint route in router
router.Get("/v:version/hi", handler)
// setup a GET /v:version/hi endpoint route in router
router.Post("/v:version/hi", handler)
// setup a GET /v:version/hi endpoint route in router
router.Put("/v:version/hi", handler)
// setup a GET /v:version/hi endpoint route in router
router.Delete("/v:version/hi", handler)
// setup a GET /v:version/hi endpoint route in router
router.Patch("/v:version/hi", handler)
// create a new request and response writer
r, _ := http.NewRequest("PATCH", "/v2.3/hi", nil)
w := httptest.NewRecorder()
// execute the request
router.ServeHTTP(w, r)
// Output: version: 2.3, resource: /v2.3/hi
}
func ExampleSimpleRoute() {
// new router
router := vestigo.NewRouter()
// standard http.HandlerFunc
handler := func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("version: %s, resource: %s\n", vestigo.Param(r, "version"), r.URL.Path)
}
// setup a GET /v:version/hi endpoint route in router
router.Get("/v:version/hi", handler)
// create a new request and response writer
r, _ := http.NewRequest("GET", "/v2.3/hi", nil)
w := httptest.NewRecorder()
// execute the request
router.ServeHTTP(w, r)
// Output: version: 2.3, resource: /v2.3/hi
}
func ExampleCorsRoute() {
// new router
router := vestigo.NewRouter()
// setup global cors config for router
router.SetGlobalCors(&vestigo.CorsAccessControl{
AllowOrigin: []string{"*", "test.com"}, // allow these origins
AllowCredentials: true, // credentials is allowed globally
ExposeHeaders: []string{"X-Header", "X-Y-Header"}, // Expose these headers
MaxAge: 3600 * time.Second, // Cache max age
AllowHeaders: []string{"X-Header", "X-Y-Header"}, // Allow these headers
})
// standard http.HandlerFunc
handler := func(w http.ResponseWriter, r *http.Request) {}
// setup a GET/v:version/hi endpoint route in router
router.Get("/v:version/hi", handler)
// Setup a CORS policy for a particular route
router.SetCors("/v:version/hi", &vestigo.CorsAccessControl{
AllowMethods: []string{"HEAD"},
AllowHeaders: []string{"X-Header", "X-Z-Header"},
})
// create a new request and response writer
r, _ := http.NewRequest("OPTIONS", "/v2.3/hi", nil)
// Initiate CORS
r.Header.Add("Origin", "test.com")
r.Header.Add("Access-Control-Request-Method", "HEAD")
w := httptest.NewRecorder()
// execute the request
router.ServeHTTP(w, r)
fmt.Println(w.Header().Get("Access-Control-Allow-Methods"))
// Output: HEAD
}