git.iko.soy/typeable/oauth2-proxy
on v1.1 help sign in
typeable/oauth2-proxy/options_test.go
92 lines 2.4 kB history raw
1package main
3import (
4 "net/url"
5 "strings"
6 "testing"
8 "github.com/bmizerany/assert"
9)
11func testOptions() *Options {
12 o := NewOptions()
13 o.Upstreams = append(o.Upstreams, "http://127.0.0.1:8080/")
14 o.CookieSecret = "foobar"
15 o.ClientID = "bazquux"
16 o.ClientSecret = "xyzzyplugh"
17 return o
20func errorMsg(msgs []string) string {
21 result := make([]string, 0)
22 result = append(result, "Invalid configuration:")
23 result = append(result, msgs...)
24 return strings.Join(result, "\n ")
27func TestNewOptions(t *testing.T) {
28 o := NewOptions()
29 err := o.Validate()
30 assert.NotEqual(t, nil, err)
32 expected := errorMsg([]string{
33 "missing setting: upstream",
34 "missing setting: cookie-secret",
35 "missing setting: client-id",
36 "missing setting: client-secret"})
37 assert.Equal(t, expected, err.Error())
40func TestInitializedOptions(t *testing.T) {
41 o := testOptions()
42 assert.Equal(t, nil, o.Validate())
45// Note that it's not worth testing nonparseable URLs, since url.Parse()
46// seems to parse damn near anything.
47func TestRedirectUrl(t *testing.T) {
48 o := testOptions()
49 o.RedirectUrl = "https://myhost.com/oauth2/callback"
50 assert.Equal(t, nil, o.Validate())
51 expected := &url.URL{
52 Scheme: "https", Host: "myhost.com", Path: "/oauth2/callback"}
53 assert.Equal(t, expected, o.redirectUrl)
56func TestProxyUrls(t *testing.T) {
57 o := testOptions()
58 o.Upstreams = append(o.Upstreams, "http://127.0.0.1:8081")
59 assert.Equal(t, nil, o.Validate())
60 expected := []*url.URL{
61 &url.URL{Scheme: "http", Host: "127.0.0.1:8080", Path: "/"},
62 // note the '/' was added
63 &url.URL{Scheme: "http", Host: "127.0.0.1:8081", Path: "/"},
64 }
65 assert.Equal(t, expected, o.proxyUrls)
68func TestCompiledRegex(t *testing.T) {
69 o := testOptions()
70 regexps := []string{"/foo/.*", "/ba[rz]/quux"}
71 o.SkipAuthRegex = regexps
72 assert.Equal(t, nil, o.Validate())
73 actual := make([]string, 0)
74 for _, regex := range o.CompiledRegex {
75 actual = append(actual, regex.String())
76 }
77 assert.Equal(t, regexps, actual)
80func TestCompiledRegexError(t *testing.T) {
81 o := testOptions()
82 o.SkipAuthRegex = []string{"(foobaz", "barquux)"}
83 err := o.Validate()
84 assert.NotEqual(t, nil, err)
86 expected := errorMsg([]string{
87 "error compiling regex=\"(foobaz\" error parsing regexp: " +
88 "missing closing ): `(foobaz`",
89 "error compiling regex=\"barquux)\" error parsing regexp: " +
90 "unexpected ): `barquux)`"})
91 assert.Equal(t, expected, err.Error())