forked from bunsenapp/go-selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_driver_cookie.go
139 lines (110 loc) · 3.17 KB
/
remote_driver_cookie.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package goselenium
import (
"bytes"
"encoding/json"
"fmt"
)
// AllCookiesResponse is the response returned from the AllCookies method.
type AllCookiesResponse struct {
State string `json:"state"`
Cookies []Cookie `json:"value"`
}
// CookieResponse is the response returned from the Cookie method.
type CookieResponse struct {
State string `json:"state"`
Cookie Cookie `json:"value"`
}
// Cookie represents a browser cookie.
type Cookie struct {
Name string `json:"name"`
Value string `json:"value"`
Path string `json:"path"`
Domain string `json:"domain"`
SecureOnly bool `json:"secure"`
HTTPOnly bool `json:"httpOnly"`
}
// AddCookieResponse is the result returned from calling the AddCookie method.
type AddCookieResponse struct {
State string
}
// DeleteCookieResponse is the result returned from calling the DeleteCookie
// method.
type DeleteCookieResponse struct {
State string
}
func (s *seleniumWebDriver) AllCookies() (*AllCookiesResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("AllCookies")
}
var response AllCookiesResponse
var err error
url := fmt.Sprintf("%s/session/%s/cookie", s.seleniumURL, s.sessionID)
resp, err := s.apiService.performRequest(url, "GET", nil)
if err != nil {
return nil, newCommunicationError(err, "AllCookies", url, nil)
}
err = json.Unmarshal(resp, &response)
if err != nil {
return nil, newUnmarshallingError(err, "AllCookies", string(resp))
}
return &response, nil
}
func (s *seleniumWebDriver) Cookie(name string) (*CookieResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("Cookie")
}
var response CookieResponse
var err error
url := fmt.Sprintf("%s/session/%s/cookie/%s", s.seleniumURL, s.sessionID, name)
resp, err := s.apiService.performRequest(url, "GET", nil)
if err != nil {
return nil, newCommunicationError(err, "Cookie", url, nil)
}
err = json.Unmarshal(resp, &response)
if err != nil {
return nil, newUnmarshallingError(err, "Cookie", string(resp))
}
return &response, nil
}
func (s *seleniumWebDriver) AddCookie(c *Cookie) (*AddCookieResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("AddCookie")
}
var err error
url := fmt.Sprintf("%s/session/%s/cookie", s.seleniumURL, s.sessionID)
j := map[string]Cookie{
"cookie": *c,
}
b, err := json.Marshal(j)
if err != nil {
return nil, newMarshallingError(err, "AddCookie", c)
}
body := bytes.NewReader(b)
resp, err := s.stateRequest(&request{
url: url,
body: body,
method: "POST",
callingMethod: "AddCookie",
})
if err != nil {
return nil, err
}
return &AddCookieResponse{State: resp.State}, nil
}
func (s *seleniumWebDriver) DeleteCookie(name string) (*DeleteCookieResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("DeleteCookie")
}
var err error
url := fmt.Sprintf("%s/session/%s/cookie/%s", s.seleniumURL, s.sessionID, name)
resp, err := s.stateRequest(&request{
url: url,
body: nil,
method: "DELETE",
callingMethod: "DeleteCookie",
})
if err != nil {
return nil, err
}
return &DeleteCookieResponse{State: resp.State}, nil
}