-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie_test.go
73 lines (58 loc) · 1.56 KB
/
cookie_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
package golang_web
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func SetCookie(writer http.ResponseWriter, request *http.Request) {
cookie := new(http.Cookie)
cookie.Name = "X-PLT-Code"
cookie.Value = request.URL.Query().Get("name")
cookie.Path = "/"
http.SetCookie(writer, cookie)
fmt.Fprint(writer, "Success Create Cookie")
}
func GetCookie(writer http.ResponseWriter, request *http.Request) {
cookie, err := request.Cookie("X-PLT-Code")
if err != nil {
fmt.Fprint(writer, "No Cookie")
} else {
name := cookie.Value
fmt.Fprintf(writer, "Cookie Name %s", name)
}
}
func TestCookie(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/set-cookie", SetCookie)
mux.HandleFunc("/get-cookie", GetCookie)
server := http.Server{
Addr: "localhost:8080",
Handler: mux,
}
err := server.ListenAndServe()
if err != nil {
panic(err)
}
}
func TestSetCookie(t *testing.T) {
request := httptest.NewRequest(http.MethodGet, "localhost:8080?name=Rochi", nil)
recorder := httptest.NewRecorder()
SetCookie(recorder, request)
cookies := recorder.Result().Cookies()
for _, cookie := range cookies {
fmt.Printf("%s : %s\n", cookie.Name, cookie.Value)
}
}
func TestGetCookie(t *testing.T) {
request := httptest.NewRequest(http.MethodGet, "localhost:8080?name=Rochi", nil)
cookie := new(http.Cookie)
cookie.Name = "X-PLT-Code"
cookie.Value = "Rochi Eko Pambudi"
request.AddCookie(cookie)
recorder := httptest.NewRecorder()
GetCookie(recorder, request)
body, _ := io.ReadAll(recorder.Result().Body)
fmt.Println(string(body))
}