コード例 #1
0
ファイル: admin_test.go プロジェクト: skyfii/shuttle
func (s *HTTPSuite) TestMaintenanceMode(c *C) {
	mainServer := s.backendServers[0]
	errServer := s.backendServers[1]

	svcCfg := client.ServiceConfig{
		Name:         "VHostTest1",
		Addr:         "127.0.0.1:9000",
		VirtualHosts: []string{"vhost1.test"},
		Backends: []client.BackendConfig{
			{Addr: mainServer.addr},
		},
		MaintenanceMode: true,
	}

	if err := Registry.AddService(svcCfg); err != nil {
		c.Fatal(err)
	}

	// No error page is registered, so we should just get a 503 error with no body
	checkHTTP("https://vhost1.test:"+s.httpsPort+"/addr", "vhost1.test", "", 503, c)

	// Use another backend to provide the error page
	svcCfg.ErrorPages = map[string][]int{
		"http://" + errServer.addr + "/error?code=503": []int{503},
	}

	if err := Registry.UpdateService(svcCfg); err != nil {
		c.Fatal(err)
	}

	// Get a 503 error with the cached body
	checkHTTP("https://vhost1.test:"+s.httpsPort+"/addr", "vhost1.test", errServer.addr, 503, c)

	// Turn maintenance mode off
	svcCfg.MaintenanceMode = false

	if err := Registry.UpdateService(svcCfg); err != nil {
		c.Fatal(err)
	}

	checkHTTP("https://vhost1.test:"+s.httpsPort+"/addr", "vhost1.test", mainServer.addr, 200, c)

	// Turn it back on
	svcCfg.MaintenanceMode = true

	if err := Registry.UpdateService(svcCfg); err != nil {
		c.Fatal(err)
	}

	checkHTTP("https://vhost1.test:"+s.httpsPort+"/addr", "vhost1.test", errServer.addr, 503, c)
}
コード例 #2
0
ファイル: admin_test.go プロジェクト: skyfii/shuttle
func (s *HTTPSuite) TestErrorPage(c *C) {
	svcCfg := client.ServiceConfig{
		Name:         "VHostTest",
		Addr:         "127.0.0.1:9000",
		VirtualHosts: []string{"test-vhost"},
	}

	okServer := s.backendServers[0]
	errServer := s.backendServers[1]

	// Add one backend to service requests
	cfg := client.BackendConfig{
		Addr: okServer.addr,
		Name: okServer.addr,
	}
	svcCfg.Backends = append(svcCfg.Backends, cfg)

	// use another backend to provide the error page
	svcCfg.ErrorPages = map[string][]int{
		"http://" + errServer.addr + "/error": []int{400, 503},
	}

	err := Registry.AddService(svcCfg)
	if err != nil {
		c.Fatal(err)
	}

	// check that the normal response comes from srv1
	checkHTTP("http://"+s.httpAddr+"/addr", "test-vhost", okServer.addr, 200, c)
	// verify that an unregistered error doesn't give the cached page
	checkHTTP("http://"+s.httpAddr+"/error?code=504", "test-vhost", okServer.addr, 504, c)
	// now see if the registered error comes from srv2
	checkHTTP("http://"+s.httpAddr+"/error?code=503", "test-vhost", errServer.addr, 503, c)

	// now check that we got the header cached in the error page as well
	req, err := http.NewRequest("GET", "http://"+s.httpAddr+"/error?code=503", nil)
	if err != nil {
		c.Fatal(err)
	}

	req.Host = "test-vhost"
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		c.Fatal(err)
	}

	c.Assert(resp.StatusCode, Equals, 503)
	c.Assert(resp.Header.Get("Last-Modified"), Equals, errServer.addr)
}