Skip to content

Commit

Permalink
fixed tests after fetch_definition refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Dino Omanovic committed Dec 14, 2016
1 parent 6eda1e8 commit e88fd8f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
30 changes: 14 additions & 16 deletions composition/content_fetcher_test.go
Expand Up @@ -3,9 +3,9 @@ package composition
import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"sort"
"testing"
"time"
"sort"
)

func Test_ContentFetcher_FetchingWithDependency(t *testing.T) {
Expand Down Expand Up @@ -77,26 +77,25 @@ func getFetchDefinitionMock(ctrl *gomock.Controller, loaderMock *MockContentLoad
}

func Test_ContentFetchResultPrioritySort(t *testing.T) {
a := assert.New(t)
a := assert.New(t)

barFd := NewFetchDefinitionWithPriority("/bar", 30)
fooFd := NewFetchDefinitionWithPriority("/foo", 10)
bazzFd := NewFetchDefinitionWithPriority("/bazz", 5)
barFd := NewFetchDefinition("/bar").WithPriority(30)
fooFd := NewFetchDefinition("/foo").WithPriority(10)
bazzFd := NewFetchDefinition("/bazz").WithPriority(5)

results := []*FetchResult{{Def: barFd}, {Def: fooFd}, {Def: bazzFd}}
results := []*FetchResult{{Def: barFd}, {Def: fooFd}, {Def: bazzFd}}

a.Equal(30, results[0].Def.Priority)
a.Equal(10, results[1].Def.Priority)
a.Equal(5, results[2].Def.Priority)
a.Equal(30, results[0].Def.Priority)
a.Equal(10, results[1].Def.Priority)
a.Equal(5, results[2].Def.Priority)

sort.Sort(FetchResults(results))
sort.Sort(FetchResults(results))

a.Equal(5, results[0].Def.Priority)
a.Equal(10, results[1].Def.Priority)
a.Equal(30, results[2].Def.Priority)
a.Equal(5, results[0].Def.Priority)
a.Equal(10, results[1].Def.Priority)
a.Equal(30, results[2].Def.Priority)
}


func Test_ContentFetcher_PriorityOrderAfterFetchCompletion(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
Expand All @@ -117,11 +116,10 @@ func Test_ContentFetcher_PriorityOrderAfterFetchCompletion(t *testing.T) {
fetcher.AddFetchJob(fooFd)
fetcher.AddFetchJob(bazzFd)


results := fetcher.WaitForResults()

a.Equal(211, results[0].Def.Priority)
a.Equal(412, results[1].Def.Priority)
a.Equal(1024, results[2].Def.Priority)

}
}
32 changes: 15 additions & 17 deletions composition/fetch_definition_test.go
Expand Up @@ -4,10 +4,10 @@ import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"net/url"
"strings"
"testing"
"time"
"net/url"
)

func Test_FetchDefinition_NewFetchDefinitionFromRequest(t *testing.T) {
Expand All @@ -18,14 +18,15 @@ func Test_FetchDefinition_NewFetchDefinitionFromRequest(t *testing.T) {
a.NoError(err)

r.Header = http.Header{
"Content-Type": {"text/html"},
"Cookie": {"aa=bb;"},
"Content-Type": {"text/html"},
"Cookie": {"aa=bb;"},
"X-Feature-Toggle": {"true"},
"Accept-Encoding": {"gzip"}, // should not be copied
"Accept-Encoding": {"gzip"}, // should not be copied
"X-Correlation-Id": {"foobar123"},
}

fd := NewFetchDefinitionFromRequest("http://upstream:8080/", r)
fd := NewFetchDefinition("http://upstream:8080/")
fd.FromRequest(r)
a.Equal("http://upstream:8080/content?foo=bar", fd.URL)
a.Equal(10*time.Second, fd.Timeout)
a.Equal(true, fd.Required)
Expand All @@ -49,21 +50,19 @@ func Test_FetchDefinition_use_DefaultErrorHandler_if_not_set(t *testing.T) {
a.Equal(NewDefaultErrorHandler(), fd.ErrHandler)
}


func Test_FetchDefinition_NewFunctions_have_default_priority(t *testing.T) {
a := assert.New(t)
request := &http.Request{}
request.URL = &url.URL{}

fd1 := NewFetchDefinition("foo")
fd2 := NewFetchDefinitionFromRequest("baa", request)
fd3 := NewFetchDefinitionWithResponseProcessorFromRequest("blub", request, nil)
fd2 := NewFetchDefinition("baa").FromRequest(request)
fd3 := NewFetchDefinition("blub").WithResponseProcessor(nil).FromRequest(request)

r, err := http.NewRequest("POST", "https://example.com/content?foo=bar", nil)
a.NoError(err)

fd4 := NewFetchDefinitionWithResponseProcessorFromRequest("bla", r, nil)

fd4 := NewFetchDefinition("bla").FromRequest(r).WithResponseProcessor(nil)

a.Equal(fd1.Priority, DefaultPriority)
a.Equal(fd2.Priority, DefaultPriority)
Expand All @@ -76,20 +75,19 @@ func Test_FetchDefinition_NewFunctions_have_parameter_priority(t *testing.T) {
request := &http.Request{}
request.URL = &url.URL{}

fd1 := NewFetchDefinitionWithPriority("foo", 42)
fd2 := NewFetchDefinitionWithPriorityFromRequest("baa", request, 54)
fd3 := NewFetchDefinitionWithResponseProcessorAndPriorityFromRequest("blub", request, nil, 74)

fd1 := NewFetchDefinition("foo").WithPriority(42)
fd2 := NewFetchDefinition("baa").WithPriority(54).FromRequest(request)
fd3 := NewFetchDefinition("blub").WithResponseProcessor(nil).WithPriority(74).FromRequest(request)

r, err := http.NewRequest("POST", "https://example.com/content?foo=bar", nil)
a.NoError(err)

fd4 := NewFetchDefinitionWithResponseProcessorAndPriorityFromRequest("bla", r, nil, 90)
fd5 := NewFetchDefinitionWithPriorityFromRequest("faa", r, 2014)
fd4 := NewFetchDefinition("bla").WithResponseProcessor(nil).WithPriority(90).FromRequest(r)
fd5 := NewFetchDefinition("faa").FromRequest(r).WithPriority(2014)

a.Equal(fd1.Priority, 42)
a.Equal(fd2.Priority, 54)
a.Equal(fd3.Priority, 74)
a.Equal(fd4.Priority, 90)
a.Equal(fd5.Priority, 2014)
}
}
2 changes: 1 addition & 1 deletion composition/http_content_loader_test.go
Expand Up @@ -71,7 +71,7 @@ func Test_HttpContentLoader_Load_ResponseProcessor(t *testing.T) {

mockResponseProcessor := NewMockResponseProcessor(ctrl)
mockResponseProcessor.EXPECT().Process(gomock.Any(), gomock.Any())
c, err := loader.Load(NewFetchDefinitionWithResponseProcessorFromRequest(server.URL, request, mockResponseProcessor))
c, err := loader.Load(NewFetchDefinition(server.URL).WithResponseProcessor(mockResponseProcessor).FromRequest(request))
a.NoError(err)
a.NotNil(c)
a.Nil(c.Reader())
Expand Down

0 comments on commit e88fd8f

Please sign in to comment.