コード例 #1
0
func Test_CompositionHandler_PositiveCaseWithCache(t *testing.T) {
	ctrl := gomock.NewController(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("Hello World\n"),
					},
				},
				Hash: "hashString",
			},
		}
	}
	ch := NewCompositionHandlerWithCache(ContentFetcherFactory(contentFetcherFactory), cache.NewCache("my-cache", 100, 100, time.Millisecond))

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

	_, foundInCache := ch.cache.Get("hashString")
	a.True(foundInCache)

}
コード例 #2
0
func Test_CompositionHandler_ErrorInMergingWithCache(t *testing.T) {
	ctrl := gomock.NewController(t)
	defer ctrl.Finish()
	a := assert.New(t)

	contentFetcherFactory := func(r *http.Request) FetchResultSupplier {
		return MockFetchResultSupplier{
			&FetchResult{
				Def:     NewFetchDefinition("/foo"),
				Content: &MemoryContent{},
				Err:     nil,
				Hash:    "hashString",
			},
		}
	}

	aggregator := NewCompositionHandlerWithCache(ContentFetcherFactory(contentFetcherFactory), cache.NewCache("my-cache", 100, 100, time.Millisecond))
	aggregator.cache.Set("hashString", "", 1, nil)
	aggregator.contentMergerFactory = func(jsonData map[string]interface{}) ContentMerger {
		merger := NewMockContentMerger(ctrl)
		merger.EXPECT().AddContent(gomock.Any(), 0)
		merger.EXPECT().GetHtml().Return(nil, errors.New("an error"))
		return merger
	}

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

	aggregator.ServeHTTP(resp, r)

	_, foundInCache := aggregator.cache.Get("hashString")

	a.False(foundInCache)
	a.Equal("Internal Server Error: an error\n", string(resp.Body.Bytes()))
	a.Equal(500, resp.Code)
}