コード例 #1
0
ファイル: router_test.go プロジェクト: Altoros/gorouter
func (s *RouterSuite) TestProxyPutRequest(c *C) {
	app := test.NewTestApp([]string{"greet.vcap.me"}, s.Config.Port, s.natsClient, nil)

	var rr *http.Request
	var msg string
	app.AddHandler("/", func(w http.ResponseWriter, r *http.Request) {
		rr = r
		b, err := ioutil.ReadAll(r.Body)
		if err != nil {
			w.WriteHeader(http.StatusBadRequest)
			return
		}
		msg = string(b)
	})
	app.Listen()
	c.Assert(s.waitAppRegistered(app, time.Second*5), Equals, true)

	url := app.Endpoint()

	buf := bytes.NewBufferString("foobar")
	r, err := http.NewRequest("PUT", url, buf)
	c.Assert(err, IsNil)

	resp, err := http.DefaultClient.Do(r)
	c.Assert(err, IsNil)
	c.Assert(resp.StatusCode, Equals, http.StatusOK)

	c.Assert(rr, NotNil)
	c.Assert(rr.Method, Equals, "PUT")
	c.Assert(rr.Proto, Equals, "HTTP/1.1")
	c.Assert(msg, Equals, "foobar")
}
コード例 #2
0
ファイル: router_test.go プロジェクト: hfeeki/gorouter
func (s *RouterSuite) TestXFF(c *C) {
	var request http.Request
	// dummy backend that records the request
	app := test.NewTestApp([]string{"xff.vcap.me"}, s.proxyPort, s.natsClient, nil)
	app.AddHandler("/", func(w http.ResponseWriter, r *http.Request) {
		request = *r
	})
	app.Listen()
	c.Assert(s.waitAppRegistered(app, time.Second*5), Equals, true)

	r, err := http.NewRequest("GET", fmt.Sprintf("http://%s:%d", "xff.vcap.me", s.proxyPort), nil)
	c.Assert(err, IsNil)
	r.Header.Set("X-Forwarded-For", "1.2.3.4")
	resp, err := http.DefaultClient.Do(r)
	c.Assert(err, IsNil)
	c.Check(resp.StatusCode, Equals, http.StatusOK)
	c.Check(strings.HasPrefix(request.Header.Get("X-Forwarded-For"), "1.2.3.4, "), Equals, true)
	app.Unregister()
	c.Assert(s.waitAppUnregistered(app, time.Second*5), Equals, true)
}
コード例 #3
0
ファイル: router_test.go プロジェクト: vito/gorouter
func (s *RouterSuite) Test100ContinueRequest(c *C) {
	app := test.NewTestApp([]string{"foo.vcap.me"}, s.Config.Port, s.mbusClient, nil)
	rCh := make(chan *http.Request)
	app.AddHandler("/", func(w http.ResponseWriter, r *http.Request) {
		_, err := ioutil.ReadAll(r.Body)
		if err != nil {
			w.WriteHeader(http.StatusBadRequest)
		}
		rCh <- r
	})

	<-s.WaitUntilNatsIsUp()

	app.Listen()
	c.Assert(s.waitAppRegistered(app, time.Second*5), Equals, true)

	host := fmt.Sprintf("foo.vcap.me:%d", s.Config.Port)
	conn, err := net.Dial("tcp", host)
	c.Assert(err, IsNil)
	defer conn.Close()

	fmt.Fprintf(conn, "POST / HTTP/1.1\r\n"+
		"Host: %s\r\n"+
		"Connection: close\r\n"+
		"Content-Length: 1\r\n"+
		"Expect: 100-continue\r\n"+
		"\r\n", host)

	fmt.Fprintf(conn, "a")

	buf := bufio.NewReader(conn)
	line, err := buf.ReadString('\n')
	c.Assert(err, IsNil)
	c.Assert(strings.Contains(line, "100 Continue"), Equals, true)

	rr := <-rCh
	c.Assert(rr, NotNil)
	c.Assert(rr.Header.Get("Expect"), Equals, "")
}