-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_pattern_test.go
60 lines (43 loc) · 1.86 KB
/
router_pattern_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
package main
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/julienschmidt/httprouter"
"github.com/stretchr/testify/assert"
)
// ============================================================================================== //
// 4. Buat parameter dlm httprouter ada 2 jenis, yg pertama itu named parameter spt dibawah
// Kalo mau ambil variabel dari pattern 1 per 1, pakai titik dua (:)
func TestRouterPatternNamedParameter(t *testing.T) {
router := httprouter.New()
router.GET("/products/:id/items/:itemId", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
productId := p.ByName("id")
itemId := p.ByName("itemId")
fmt.Fprint(w, "Product "+productId+" Item "+itemId)
})
request := httptest.NewRequest(http.MethodGet, "http://localhost:2020/products/1/items/2", nil)
recorder := httptest.NewRecorder()
router.ServeHTTP(recorder, request)
response := recorder.Result()
body, _ := io.ReadAll(response.Body)
assert.Equal(t, "Product 1 Item 2", string(body))
}
// ============================================================================================== //
// Yg kedua AllParameter spt dibawah
// Kalo mau ambil semua variabel / item dlm url stlh titik tertentu pakai bintang (*)
func TestRouterPatternAllParameter(t *testing.T) {
router := httprouter.New()
router.GET("/images/*image", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
fmt.Fprint(w, "Image : "+p.ByName("image"))
})
request := httptest.NewRequest(http.MethodGet, "http://localhost:2020/images/small/profile.png", nil)
recorder := httptest.NewRecorder()
router.ServeHTTP(recorder, request)
response := recorder.Result()
body, _ := io.ReadAll(response.Body)
assert.Equal(t, "Image : /small/profile.png", string(body))
}
// ============================================================================================== //