this seems okay
This commit is contained in:
131
main_test.go
Normal file
131
main_test.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestHTTPSMain(t *testing.T) {
|
||||
was := os.Args[:]
|
||||
os.Args = []string{"program"}
|
||||
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
|
||||
defer func() {
|
||||
os.Args = was[:]
|
||||
}()
|
||||
|
||||
addr, stop := echoServer()
|
||||
defer stop()
|
||||
ported := make(chan string)
|
||||
go func() {
|
||||
p := getPort()
|
||||
ported <- p
|
||||
os.Args = []string{
|
||||
"foobar",
|
||||
"-p",
|
||||
p,
|
||||
"-user",
|
||||
"username",
|
||||
"-pass",
|
||||
"password",
|
||||
"-r",
|
||||
"hello:" + addr,
|
||||
"-crt",
|
||||
"./testdata/rproxy3server.crt",
|
||||
"-key",
|
||||
"./testdata/rproxy3server.key",
|
||||
}
|
||||
main()
|
||||
}()
|
||||
port := <-ported
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
|
||||
client := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
},
|
||||
}
|
||||
r, _ := http.NewRequest("GET", "https://hello.localhost"+port, nil)
|
||||
|
||||
if resp, err := client.Do(r); err != nil {
|
||||
t.Fatalf("client failed: %v", err)
|
||||
} else if resp.StatusCode != http.StatusUnauthorized {
|
||||
t.Errorf("proxy failed: code %v != %v", resp.StatusCode, http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
r.SetBasicAuth("username", "password")
|
||||
if resp, err := client.Do(r); err != nil {
|
||||
t.Fatalf("client failed: %v", err)
|
||||
} else if resp.StatusCode != http.StatusOK {
|
||||
t.Errorf("proxy failed: code %v != %v", resp.StatusCode, http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPMain(t *testing.T) {
|
||||
was := os.Args[:]
|
||||
os.Args = []string{"program"}
|
||||
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
|
||||
defer func() {
|
||||
os.Args = was[:]
|
||||
}()
|
||||
|
||||
addr, stop := echoServer()
|
||||
defer stop()
|
||||
ported := make(chan string)
|
||||
go func() {
|
||||
p := getPort()
|
||||
ported <- p
|
||||
os.Args = []string{
|
||||
"foobar",
|
||||
"-p",
|
||||
p,
|
||||
"-user",
|
||||
"username",
|
||||
"-pass",
|
||||
"password",
|
||||
"-r",
|
||||
"hello:" + addr,
|
||||
}
|
||||
main()
|
||||
}()
|
||||
port := <-ported
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
|
||||
client := &http.Client{}
|
||||
r, _ := http.NewRequest("GET", "http://hello.localhost"+port, nil)
|
||||
|
||||
if resp, err := client.Do(r); err != nil {
|
||||
t.Fatalf("client failed: %v", err)
|
||||
} else if resp.StatusCode != http.StatusUnauthorized {
|
||||
t.Errorf("proxy failed: code %v != %v", resp.StatusCode, http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
r.SetBasicAuth("username", "password")
|
||||
if resp, err := client.Do(r); err != nil {
|
||||
t.Fatalf("client failed: %v", err)
|
||||
} else if resp.StatusCode != http.StatusOK {
|
||||
t.Errorf("proxy failed: code %v != %v", resp.StatusCode, http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
func echoServer() (string, func()) {
|
||||
h := func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "hello")
|
||||
}
|
||||
portsrv := httptest.NewServer(http.HandlerFunc(h))
|
||||
return portsrv.URL, func() {
|
||||
portsrv.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func getPort() string {
|
||||
s := httptest.NewServer(nil)
|
||||
s.Close()
|
||||
return s.URL[strings.LastIndex(s.URL, ":"):]
|
||||
}
|
||||
Reference in New Issue
Block a user