-
Notifications
You must be signed in to change notification settings - Fork 0
/
method_not_allowed_test.go
43 lines (32 loc) · 1.34 KB
/
method_not_allowed_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
package main
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/julienschmidt/httprouter"
"github.com/stretchr/testify/assert"
)
// ============================================================================================== //
// 8. Kl user menggunakan method yg beda dr yg kita tentukan. Contoh: kita ingin menggunakan method
// GET pada suatu halaman, tetapi user malah mengirimkan method POST, maka httprouter sbnrnya
// sudah secara otomatis mengarahkan ke halaman error not allowed. Ini kita lakukan kl mau custom page
// error-nya
func TestMethodNotAllowed(t *testing.T) {
router := httprouter.New()
router.MethodNotAllowed = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Method yang digunakan tidak sesuai >:(")
})
router.POST("/", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
fmt.Fprint(w, "Method POST")
})
// Manggil method get, HARUSNYA panggil method POST
request := httptest.NewRequest(http.MethodPut, "http://localhost:2020/", nil)
recorder := httptest.NewRecorder()
router.ServeHTTP(recorder, request)
response := recorder.Result()
body, _ := io.ReadAll(response.Body)
assert.Equal(t, "Method yang digunakan tidak sesuai >:(", string(body))
}
// ============================================================================================== //