Skip to content

Commit

Permalink
Merge c51e0a8 into d4e78f0
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Mancke committed Nov 21, 2016
2 parents d4e78f0 + c51e0a8 commit fecbb91
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
18 changes: 17 additions & 1 deletion composition/composition_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ func (agg *CompositionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
}
}

// But also allow other fetch definitions to set cookies, if Set-Cookie ist globaly allowed
if len(results) > 1 && contains(ForwardResponseHeaders, "Set-Cookie") {
for _, r := range results[1:] {
copyHeaders(r.Content.HttpHeader(), w.Header(), []string{"Set-Cookie"})
}
}

// Overwrite Content-Type to ensure, that the encoding is correct
w.Header().Set("Content-Type", "text/html; charset=utf-8")

Expand Down Expand Up @@ -150,7 +157,16 @@ func getHostFromRequest(r *http.Request) string {

func hasPrioritySetting(results []*FetchResult) bool {
for _, res := range results {
if(res.Def.Priority > 0){
if res.Def.Priority > 0 {
return true
}
}
return false
}

func contains(list []string, item string) bool {
for _, v := range list {
if item == v {
return true
}
}
Expand Down
55 changes: 52 additions & 3 deletions composition/composition_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func Test_CompositionHandler_PositiveCaseWithCache(t *testing.T) {
"": StringFragment("Hello World\n"),
},
},
Hash: "hashString",
Hash: "hashString",
},
}
}
Expand All @@ -88,6 +88,56 @@ func Test_CompositionHandler_CorrectHeaderAndStatusCodeReturned(t *testing.T) {
defer ctrl.Finish()
a := assert.New(t)

contentFetcherFactory := func(r *http.Request) FetchResultSupplier {
return MockFetchResultSupplier{
&FetchResult{
Def: NewFetchDefinition("/foo"),
Content: &MemoryContent{
body: map[string]Fragment{
"": StringFragment(""),
},
httpHeader: http.Header{
"Transfer-Encoding": {"gzip"}, // removed
"Set-Cookie": {
"cookie-content 1",
"cookie-content 2",
},
},
httpStatusCode: 201,
},
},
&FetchResult{
Def: NewFetchDefinition("..."),
Content: &MemoryContent{
httpHeader: http.Header{
"Set-Cookie": {
"cookie-content 3",
},
},
httpStatusCode: 200,
},
},
}
}
ch := NewCompositionHandler(ContentFetcherFactory(contentFetcherFactory))

resp := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "http://example.com", nil)
ch.ServeHTTP(resp, r)

a.Equal(201, resp.Code)
a.Equal(3, len(resp.Header())) // Set-Cookie + Content-Type + Content-Lenth
a.Equal("", resp.Header().Get("Transfer-Encoding"))
a.Contains(resp.Header()["Set-Cookie"], "cookie-content 1")
a.Contains(resp.Header()["Set-Cookie"], "cookie-content 2")
a.Contains(resp.Header()["Set-Cookie"], "cookie-content 3")
}

func Test_CompositionHandler_CorrectHeaderAndStatusCodeReturnedOnRedirect(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
a := assert.New(t)

contentFetcherFactory := func(r *http.Request) FetchResultSupplier {
return MockFetchResultSupplier{
&FetchResult{
Expand All @@ -108,8 +158,7 @@ func Test_CompositionHandler_CorrectHeaderAndStatusCodeReturned(t *testing.T) {
},
},
&FetchResult{
Def: NewFetchDefinition("..."),
Content: &MemoryContent{},
Def: NewFetchDefinition("..."),
},
}
}
Expand Down
11 changes: 6 additions & 5 deletions composition/fetch_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

const MAX_PRIORITY int = 4294967295

// ForwardRequestHeaders are those headers,
// which are incuded from the original client request to the backend request.
// TODO: Add Host header to an XFF header
Expand Down Expand Up @@ -52,9 +53,9 @@ var ForwardResponseHeaders = []string{
"WWW-Authenticate"}

const (
DefaultTimeout time.Duration = 10 * time.Second
FileURLPrefix = "file://"
DefaultPriority = 0
DefaultTimeout time.Duration = 10 * time.Second
FileURLPrefix = "file://"
DefaultPriority = 0
)

// FetchDefinition is a descriptor for fetching Content from an endpoint.
Expand Down Expand Up @@ -99,7 +100,7 @@ func NewFetchDefinitionWithErrorHandlerAndPriority(url string, errHandler ErrorH
Method: "GET",
ErrHandler: errHandler,
CacheStrategy: cache.DefaultCacheStrategy,
Priority: priority,
Priority: priority,
}
}

Expand Down Expand Up @@ -194,7 +195,7 @@ func (def *FetchDefinition) IsReadableFromCache() bool {
}

// copyHeaders copies only the header contained in the the whitelist
// from src to test. If dest is nil, it will be created.
// from src to dest. If dest is nil, it will be created.
// The dest will also be returned.
func copyHeaders(src, dest http.Header, whitelist []string) http.Header {
if dest == nil {
Expand Down

0 comments on commit fecbb91

Please sign in to comment.