Exemplo n.º 1
0
// initFromConfig should be called when starting or reloading the app. It makes
// all the connections between cache zones, virtual hosts and upstreams.
func (a *Application) initFromConfig() (err error) {
	// Make the vhost and cacheZone maps
	a.cacheZones = make(map[string]*types.CacheZone)

	a.ctx, a.ctxCancel = context.WithCancel(context.Background())
	a.ctx = contexts.NewAppContext(a.ctx, a)
	a.ctx = contexts.NewCacheZonesContext(a.ctx, a.cacheZones)
	return a.reinitFromConfig()
}
Exemplo n.º 2
0
func testSetupWithStorage(t *testing.T, st types.Storage) (context.Context, *Handler, *types.Location) {
	var cz = &types.CacheZone{
		ID: "testZoen",
		Algorithm: mock.NewCacheAlgorithm(&mock.CacheAlgorithmRepliers{
			Remove: removeFunctionMock(t),
		}),
		Storage: st,
	}
	loc1 := &types.Location{
		Logger:   mock.NewLogger(),
		Cache:    cz,
		CacheKey: cacheKey1,
		Name:     "location1",
	}
	loc2 := &types.Location{
		Logger:   mock.NewLogger(),
		Cache:    cz,
		CacheKey: cacheKey2,
		Name:     "location2",
	}
	app := &mockApp{
		getLocationFor: func(host, path string) *types.Location {
			if host == host1 {
				return loc1
			}
			if host == host2 {
				return loc2
			}

			return nil
		},
	}
	loc3 := &types.Location{
		Logger: mock.NewLogger(),
	}

	ctx := contexts.NewAppContext(context.Background(), app)
	purger, err := New(&config.Handler{}, loc3, nil)
	if err != nil {
		t.Fatal(err)
	}
	return ctx, purger, loc3
}
Exemplo n.º 3
0
func TestSimpleRetryWithNilUpstream(t *testing.T) {
	t.Parallel()
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path == "/err" {
			w.WriteHeader(404)
			fmt.Fprint(w, "error!")
			return
		}
		w.WriteHeader(200)
		fmt.Fprint(w, "hello world")
	}))
	defer ts.Close()

	upstreamURL, err := url.Parse(ts.URL)
	if err != nil {
		t.Fatal(err)
	}
	up, err := upstream.NewSimple(upstreamURL)
	if err != nil {
		t.Fatal(err)
	}

	retryTs := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path == "/err" {
			w.WriteHeader(200)
			fmt.Fprint(w, "not error!")
			return
		}
		w.WriteHeader(404)
		fmt.Fprint(w, "not hello world")
	}))
	defer retryTs.Close()

	retryUpstreamURL, err := url.Parse(retryTs.URL)
	if err != nil {
		t.Fatal(err)
	}
	retryUpstream, err := upstream.NewSimple(retryUpstreamURL)
	if err != nil {
		t.Fatal(err)
	}

	proxy, err := New(
		config.NewHandler("proxy", json.RawMessage(`{ "try_other_upstream_on_code" : {"404": "nonexistant_upstream"}}`)),
		&types.Location{
			Name:     "test",
			Logger:   mock.NewLogger(),
			Upstream: up,
		}, nil)
	if err != nil {
		t.Fatal(err)
	}

	req1, err := http.NewRequest("GET", "http://www.somewhere.com/err", nil)
	if err != nil {
		t.Fatal(err)
	}
	resp1 := httptest.NewRecorder()
	ctx := contexts.NewAppContext(context.Background(), &mockApp{
		upstreams: map[string]types.Upstream{
			"retry_upstream": retryUpstream,
		},
	})
	proxy.ServeHTTP(ctx, resp1, req1)
	if resp1.Code != 404 || resp1.Body.String() != "error!" {
		t.Errorf("Unexpected response %#v", resp1)
	}

	req2, err := http.NewRequest("GET", "http://www.somewhere.com/index", nil)
	if err != nil {
		t.Fatal(err)
	}
	resp2 := httptest.NewRecorder()
	proxy.ServeHTTP(nil, resp2, req2)
	if resp2.Code != 200 || resp2.Body.String() != "hello world" {
		t.Errorf("Unexpected response %#v", resp2)
	}
}