예제 #1
0
func TestBasicConnectAuthWithCurl(t *testing.T) {
	expected := ":c>"
	background := httptest.NewTLSServer(ConstantHanlder(expected))
	defer background.Close()
	proxy := goproxy.NewProxyHttpServer()
	proxy.OnRequest().HandleConnect(auth.BasicConnect("my_realm", func(user, passwd string) bool {
		return user == "user" && passwd == "open sesame"
	}))
	_, proxyserver := oneShotProxy(proxy)
	defer proxyserver.Close()

	cmd := exec.Command("curl",
		"--silent", "--show-error", "--insecure",
		"-x", proxyserver.URL,
		"-U", "user:open sesame",
		"-p",
		"--url", background.URL+"/[1-3]",
	)
	out, err := cmd.CombinedOutput() // if curl got error, it'll show up in stderr
	if err != nil {
		t.Fatal(err, string(out))
	}
	finalexpected := times(3, expected)
	if string(out) != finalexpected {
		t.Error("Expected", finalexpected, "got", string(out))
	}
}
		Context("with a message in the trafficcontroller", func() {
			BeforeEach(func() {
				messagesToSend <- marshalMessage(createMessage("hello", 0))
			})

			It("connects using valid URL to running proxy server", func() {
				message := <-incoming
				Expect(message.GetLogMessage().GetMessage()).To(Equal([]byte("hello")))
			})
		})

		Context("with an auth proxy server", func() {
			BeforeEach(func() {
				goProxyHandler.OnRequest().HandleConnect(auth.BasicConnect("my_realm", func(user, passwd string) bool {
					return user == "user" && passwd == "password"
				}))
				proxy = func(*http.Request) (*url.URL, error) {
					proxyURL, err := url.Parse(testProxyServer.URL)
					proxyURL.User = url.UserPassword("user", "password")
					if err != nil {
						return nil, err
					}

					return proxyURL, nil
				}
				messagesToSend <- marshalMessage(createMessage("hello", 0))
			})

			It("connects successfully", func() {
				message := <-incoming