Skip to content

Commit

Permalink
Fixed logic for when cookie should be set
Browse files Browse the repository at this point in the history
The correct logic is that it should be set if the current set version does not exist, not if it exist
  • Loading branch information
lindell committed Jul 26, 2020
1 parent a16b6c4 commit fcd2ce7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/revaboxy/reverse_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func New(vv []Version, settingChangers ...Setting) (*Revaboxy, error) {
name := r.Request.Header.Get(settings.headerName)
existingCookie, _ := r.Request.Cookie(settings.cookieName)

if name != "" && (existingCookie == nil || versions.get(existingCookie.Value) != nil) {
if name != "" && (existingCookie == nil || versions.get(existingCookie.Value) == nil) {
newCookie := &http.Cookie{
Name: settings.cookieName,
Value: name,
Expand Down
10 changes: 7 additions & 3 deletions pkg/revaboxy/reverse_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/http/httptest"
"net/url"
"strings"
Expand Down Expand Up @@ -41,6 +42,9 @@ func TestReverseProxyWithCookies(t *testing.T) {
// Setup the server
url1, _ := url.Parse("http://url1.test/path1")
url2, _ := url.Parse("http://url2.test/path2")

cookieJar, _ := cookiejar.New(nil)

proxy, err := New(
[]Version{
{
Expand Down Expand Up @@ -83,13 +87,13 @@ func TestReverseProxyWithCookies(t *testing.T) {
t.Fatal("could not read body")
}

cookies := recorder.Result().Cookies()
cookieJar.SetCookies(req.URL, recorder.Result().Cookies())

// Makes sure that subsequent requests uses the same path
for i := 0; i < 10; i++ {
recorder := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, "http://baseurl.com/basepath", nil)
for _, c := range cookies {
for _, c := range cookieJar.Cookies(req.URL) {
req.AddCookie(c)
}
if err != nil {
Expand All @@ -108,7 +112,7 @@ func TestReverseProxyWithCookies(t *testing.T) {
t.Fatalf("expected body to be equal to the first body (%s) but got %s", expected, real)
}

cookies = recorder.Result().Cookies()
cookieJar.SetCookies(req.URL, recorder.Result().Cookies())
}
}

Expand Down

0 comments on commit fcd2ce7

Please sign in to comment.