Created
February 17, 2022 01:49
-
-
Save plvhx/0e7f63eb3765155641f8b737a399576e to your computer and use it in GitHub Desktop.
http.CookieJar interface implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"reflect" | |
"net/http" | |
"net/url" | |
) | |
type CookieJar struct { | |
url *url.URL | |
cookies []*http.Cookie | |
} | |
func (c *CookieJar) SetCookies(url *url.URL, cookies []*http.Cookie) { | |
c.url = url | |
c.cookies = cookies | |
} | |
func (c *CookieJar) Cookies(url *url.URL) []*http.Cookie { | |
if !reflect.DeepEqual(url, c.url) { | |
return nil | |
} | |
return c.cookies | |
} | |
func main() { | |
url := &url.URL{ | |
Scheme: "https", | |
Opaque: "foobarbaz", | |
User: url.UserPassword("foo", "bar"), | |
Host: "sub.foo.com", | |
Path: "/shit_this_is_happening_again", | |
RawPath: "", | |
ForceQuery: true, | |
RawQuery: "foo=foo&bar=bar", | |
Fragment: "sdfsdfsdfsdffdsfdsfdsfds", | |
} | |
cookies := []*http.Cookie{ | |
&http.Cookie{ | |
Name: "foo", | |
Value: "this_is_a_foo", | |
MaxAge: 3600, | |
Secure: false, | |
HttpOnly: true, | |
SameSite: http.SameSiteDefaultMode, | |
Raw: "", | |
Unparsed: []string{"foo=foo", "bar=bar"}, | |
}, | |
&http.Cookie{ | |
Name: "bar", | |
Value: "this_is_a_bar", | |
MaxAge: 3600, | |
Secure: false, | |
HttpOnly: true, | |
SameSite: http.SameSiteDefaultMode, | |
Raw: "", | |
Unparsed: []string{"foo=foo", "bar=bar"}, | |
}, | |
} | |
jar := &CookieJar{} | |
jar.SetCookies(url, cookies) | |
fmt.Printf("%+v\n", jar.Cookies(url)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment