Skip to content

Commit

Permalink
Add test cases for SNIMatch
Browse files Browse the repository at this point in the history
  • Loading branch information
tmthrgd committed Nov 16, 2017
1 parent 49c7556 commit 3d967b1
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions sni-match_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2017 Tom Thorogood. All rights reserved.
// Use of this source code is governed by a
// Modified BSD License that can be found in
// the LICENSE file.

package handlers

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

func TestSNIMatch(t *testing.T) {
h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(999)
})
h2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(998)
})

r := httptest.NewRequest(http.MethodGet, "http://example.com/path/to/file", nil)
w := httptest.NewRecorder()
SNIMatch(h1, h2).ServeHTTP(w, r)

assert.Equal(t, 999, w.Code, "SNIMatch invoked incorrect http.Handler")

r = httptest.NewRequest(http.MethodGet, "https://example.com/path/to/file", nil)
r.TLS.ServerName = ""
w = httptest.NewRecorder()
SNIMatch(h1, h2).ServeHTTP(w, r)

assert.Equal(t, 999, w.Code, "SNIMatch invoked incorrect http.Handler")

r = httptest.NewRequest(http.MethodGet, "https://example.com/path/to/file", nil)
r.ProtoMajor = 2
w = httptest.NewRecorder()
SNIMatch(h1, h2).ServeHTTP(w, r)

assert.Equal(t, 999, w.Code, "SNIMatch invoked incorrect http.Handler")

r = httptest.NewRequest(http.MethodGet, "https://example.com:1234/path/to/file", nil)
// httptest.NewRequest does not properly strip the
// port before setting r.TLS.ServerName.
r.Host, r.TLS.ServerName = "example.com:1234", "example.com"
w = httptest.NewRecorder()
SNIMatch(h1, h2).ServeHTTP(w, r)

assert.Equal(t, 999, w.Code, "SNIMatch invoked incorrect http.Handler")
}

func TestSNIMatchMismatch(t *testing.T) {
h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(999)
})
h2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(998)
})

r := httptest.NewRequest(http.MethodGet, "https://example.com/path/to/file", nil)
r.Host = "example.org"
w := httptest.NewRecorder()
SNIMatch(h1, h2).ServeHTTP(w, r)

assert.Equal(t, 998, w.Code, "SNIMatch invoked incorrect http.Handler")

r = httptest.NewRequest(http.MethodGet, "https://example.com/path/to/file", nil)
r.TLS.ServerName = "example.org"
w = httptest.NewRecorder()
SNIMatch(h1, h2).ServeHTTP(w, r)

assert.Equal(t, 998, w.Code, "SNIMatch invoked incorrect http.Handler")

r = httptest.NewRequest(http.MethodGet, "https://example.com/path/to/file", nil)
r.Host = "example.org:1234"
w = httptest.NewRecorder()
SNIMatch(h1, h2).ServeHTTP(w, r)

assert.Equal(t, 998, w.Code, "SNIMatch invoked incorrect http.Handler")
}

0 comments on commit 3d967b1

Please sign in to comment.