Skip to content

Commit

Permalink
Add tests for TLS verification checks
Browse files Browse the repository at this point in the history
  • Loading branch information
sporkmonger committed Sep 18, 2018
1 parent 027d04a commit 9a1a35f
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions internal/proxy/oauthproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,110 @@ func TestNewReverseProxyHostname(t *testing.T) {

}

func TestNewReverseProxyTLSVerifyTrue(t *testing.T) {
type respStruct struct {
HandshakeComplete bool `json:"handshake-complete"`
}

to := httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
body, err := json.Marshal(
// Doesn't really matter what's sent since we should 502
&respStruct{
HandshakeComplete: r.TLS.HandshakeComplete,
},
)
if err != nil {
t.Fatalf("expected to marshal json: %s", err)
}
rw.Write(body)
}))
defer to.Close()

toURL, err := url.Parse(to.URL)
if err != nil {
t.Fatalf("expected to parse to url: %s", err)
}

reverseProxy := NewReverseProxy(toURL, &UpstreamConfig{TLSVerify: true})
from := httptest.NewServer(reverseProxy)
defer from.Close()

res, err := http.Get(from.URL)
if err != nil {
t.Fatalf("expected to be able to make req: %s", err)
}

if res.StatusCode != 502 {
t.Logf(" got status code: %v", res.StatusCode)
t.Logf("want status code: %d", 502)

t.Errorf("got unexpected response code for tls failure")
}
if res.Header.Get("Cookie") != "" {
t.Errorf("expected Cookie header to be empty but was %s", res.Header.Get("Cookie"))
}

}

func TestNewReverseProxyTLSVerifyFalse(t *testing.T) {
type respStruct struct {
HandshakeComplete bool `json:"handshake-complete"`
}

to := httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
body, err := json.Marshal(
&respStruct{
HandshakeComplete: r.TLS.HandshakeComplete,
},
)
if err != nil {
t.Fatalf("expected to marshal json: %s", err)
}
rw.Write(body)
}))
defer to.Close()

toURL, err := url.Parse(to.URL)
if err != nil {
t.Fatalf("expected to parse to url: %s", err)
}

reverseProxy := NewReverseProxy(toURL, &UpstreamConfig{TLSVerify: false})
from := httptest.NewServer(reverseProxy)
defer from.Close()

want := &respStruct{
HandshakeComplete: true,
}

res, err := http.Get(from.URL)
if err != nil {
t.Fatalf("expected to be able to make req: %s", err)
}

body, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatalf("expected to read body: %s", err)
}

got := &respStruct{}
err = json.Unmarshal(body, got)
if err != nil {
t.Fatalf("expected to decode json: %s", err)
}

if !reflect.DeepEqual(want, got) {
t.Logf(" got handshake complete: %v", got.HandshakeComplete)
t.Logf("want handshake complete: %v", want.HandshakeComplete)

t.Errorf("got unexpected response for handshake complete")
}
if res.Header.Get("Cookie") != "" {
t.Errorf("expected Cookie header to be empty but was %s", res.Header.Get("Cookie"))
}

}

func TestDeleteSSOHeader(t *testing.T) {
testCases := []struct {
name string
Expand Down

0 comments on commit 9a1a35f

Please sign in to comment.