-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🐛 [Bug]: There is something weird with Fiber and the usage of Http lib for testing. #3176
Comments
Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
@jujur10 I'm not following what the issue is from the code. You are sending a Can you provide a more simple reproducible example? Ican't copy/run what you provided. |
@gaby Yes, because in modern navigator and with fiber it is possible to have json into cookies but with http library, it is impossible to have |
As a proper workaround (that doesn't involve turning the JSON data into base 64), you can try setting the Cookie header directly. req.Header.Set("Cookie", "oauth_state="+string(marshaledState)) // add your other cookies You can write a small cookie jar utility to make your life easier and avoid repetition (note that this is a very primitive implementation that doesn't do any checks/sanitization): package main
import "net/http"
type CookieJar struct {
cookies map[string]string
}
func NewCookieJar() *CookieJar {
return &CookieJar{
cookies: make(map[string]string),
}
}
func (c *CookieJar) Set(key, value string) {
c.cookies[key] = value
}
func (c *CookieJar) AddCookie(cookie *http.Cookie) {
c.cookies[cookie.Name] = cookie.Value
}
func (c *CookieJar) Parse() string {
var cookie string
for key, value := range c.cookies {
cookie += key + "=" + value + ";"
}
return cookie
}
func (c *CookieJar) AddToRequest(req *http.Request) {
req.Header.Set("Cookie", c.Parse())
} Usage in your tests: cookiejar := NewCookieJar()
if sessionCookie != nil {
cookiejar.AddCookie(sessionCookie)
}
cookiejar.Set("oauth_state", string(marshaledState))
cookiejar.AddToRequest(req) As for the testing library, do you have any alternative in mind that Fiber could use instead of |
Bug Description
You are using the HTTP library (https://docs.gofiber.io/api/app/#test) but some fiber features are not RFC compliant, example with cookies using the
"
character. You can't use HTTP library if you have to send (and the backend have to receive) a golang json structure.If you want to stock data in cookies (very practical but non RFC compliant).
How to Reproduce
"
.Expected Behavior
The cookie with
"
are supposed to work.Fiber Version
v2.52.5
Code Snippet (optional)
For this part:
It is mandatory to convert to base 64 in order to handle the
"
in the backend.Checklist:
The text was updated successfully, but these errors were encountered: