-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (53 loc) · 1.24 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
54
55
56
57
58
59
60
61
package main
import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"time"
"github.com/sclevine/agouti"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", handleRoot)
mux.HandleFunc("/cookie", handleCookie)
server := httptest.NewServer(mux)
defer server.Close()
driver := agouti.ChromeDriver()
//driver := agouti.PhantomJS()
if err := driver.Start(); err != nil {
log.Fatal(err)
}
defer driver.Stop() //nolint:errcheck
page, err := driver.NewPage()
if err != nil {
log.Fatal(err)
}
if err := page.Navigate(server.URL + "/cookie"); err != nil {
log.Fatal(err)
}
time.Sleep(time.Second * 3)
cookies, err := page.GetCookies()
if err != nil {
log.Fatal(err)
}
fmt.Printf("cookies = %+v\n", cookies)
}
func handleRoot(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("test")
if err != nil {
http.Error(w, fmt.Sprintf("No cookie\n\n%v", err), http.StatusInternalServerError)
}
fmt.Fprintf(w, "cookie = %+v", cookie)
}
func handleCookie(w http.ResponseWriter, r *http.Request) {
cookie := &http.Cookie{
Name: "test",
Value: "test",
Path: "/",
Expires: time.Now().Add(time.Hour * 24),
HttpOnly: true,
}
http.SetCookie(w, cookie)
http.Redirect(w, r, "/", http.StatusFound)
}