Example #1
0
// Benchmark_WithInitStoreByFormCookie-4	    3000	    481881 ns/op	  189103 B/op	     232 allocs/op => with debug enabled
// Benchmark_WithInitStoreByFormCookie-4	  300000	      4797 ns/op	    1016 B/op	      16 allocs/op => debug disabled
func Benchmark_WithInitStoreByFormCookie(b *testing.B) {
	store.PkgLog.SetLevel(log.StdLevelInfo)
	b.ReportAllocs()

	wantStoreCode := "nz"
	ctx := store.NewContextReader(context.Background(), getInitializedStoreService(scope.Option{Website: scope.MockID(2)}))

	mw := store.WithInitStoreByFormCookie()(benchValidationHandler(b, wantStoreCode))

	rec := httptest.NewRecorder()
	req, err := http.NewRequest(httputils.MethodGet, "https://corestore.io/store/list/", nil)
	if err != nil {
		b.Fatal(err)
	}

	req.AddCookie(&http.Cookie{
		Name:  store.ParamName,
		Value: wantStoreCode,
	})

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		if err := mw.ServeHTTPContext(ctx, rec, req); err != nil {
			b.Error(err)
		}
	}
}
Example #2
0
func TestWithInitStoreByFormCookie(t *testing.T) {
	errLogBuf.Reset()
	defer errLogBuf.Reset()

	for i, test := range testsMWInitByFormCookie {

		ctx := store.NewContextReader(context.Background(), getInitializedStoreService(test.haveSO))

		mw := store.WithInitStoreByFormCookie()(finalInitStoreHandler(t, test.wantStoreCode))

		rec := httptest.NewRecorder()
		surfErr := mw.ServeHTTPContext(ctx, rec, test.req)
		if test.wantErr != nil {
			var loc string
			if l, ok := surfErr.(errgo.Locationer); ok {
				loc = l.Location().String()
			}

			assert.EqualError(t, surfErr, test.wantErr.Error(), "\nIndex %d\n%s", i, loc)
			errLogBuf.Reset()
			continue
		}

		if test.wantLog != "" {
			assert.Contains(t, errLogBuf.String(), test.wantLog, "\nIndex %d\n", i)
			errLogBuf.Reset()
			continue
		} else {
			assert.Empty(t, errLogBuf.String(), "\nIndex %d\n", i)
		}

		assert.NoError(t, surfErr, "Index %d", i)

		newKeks := rec.HeaderMap.Get("Set-Cookie")
		if test.wantCookie != "" {
			assert.Contains(t, newKeks, test.wantCookie, "\nIndex %d\n", i)
		} else {
			assert.Empty(t, newKeks, "%#v", test)
		}
		errLogBuf.Reset()
	}
}
Example #3
0
func TestWithInitStoreByFormCookie_NilCtx(t *testing.T) {
	mw := store.WithInitStoreByFormCookie()(nil)
	surfErr := mw.ServeHTTPContext(context.Background(), nil, nil)
	assert.EqualError(t, surfErr, store.ErrContextServiceNotFound.Error())
}