diff --git a/composition/content_fetcher_test.go b/composition/content_fetcher_test.go index bd8430d..afe780a 100644 --- a/composition/content_fetcher_test.go +++ b/composition/content_fetcher_test.go @@ -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) { @@ -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() @@ -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) -} \ No newline at end of file +} diff --git a/composition/fetch_definition_test.go b/composition/fetch_definition_test.go index 2b9f2d2..679f02f 100644 --- a/composition/fetch_definition_test.go +++ b/composition/fetch_definition_test.go @@ -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) { @@ -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) @@ -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) @@ -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) -} \ No newline at end of file +} diff --git a/composition/http_content_loader_test.go b/composition/http_content_loader_test.go index 28a2564..5b66342 100644 --- a/composition/http_content_loader_test.go +++ b/composition/http_content_loader_test.go @@ -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())