|
1 | 1 | package config |
2 | 2 |
|
3 | | -import "testing" |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
4 | 10 |
|
5 | 11 | func TestConfig(t *testing.T) { |
6 | 12 | c := &Config{} |
7 | 13 | _ = c.Context() |
| 14 | + |
| 15 | + assert.Equal(t, c.GetAccessTokenLifespan(), time.Hour) |
| 16 | +} |
| 17 | + |
| 18 | +func TestDoesRequestSatisfyTermination(t *testing.T) { |
| 19 | + c := &Config{AllowTLSTermination: ""} |
| 20 | + assert.NotNil(t, c.DoesRequestSatisfyTermination(new(http.Request))) |
| 21 | + |
| 22 | + c = &Config{AllowTLSTermination: "127.0.0.1/24"} |
| 23 | + r := &http.Request{Header: http.Header{}} |
| 24 | + assert.NotNil(t, c.DoesRequestSatisfyTermination(r)) |
| 25 | + |
| 26 | + r = &http.Request{Header: http.Header{"X-Forwarded-Proto": []string{"http"}}} |
| 27 | + assert.NotNil(t, c.DoesRequestSatisfyTermination(r)) |
| 28 | + |
| 29 | + r = &http.Request{ |
| 30 | + RemoteAddr: "227.0.0.1:123", |
| 31 | + Header: http.Header{"X-Forwarded-Proto": []string{"https"}}, |
| 32 | + } |
| 33 | + assert.NotNil(t, c.DoesRequestSatisfyTermination(r)) |
| 34 | + |
| 35 | + r = &http.Request{ |
| 36 | + RemoteAddr: "127.0.0.1:123", |
| 37 | + Header: http.Header{"X-Forwarded-Proto": []string{"https"}}, |
| 38 | + } |
| 39 | + assert.Nil(t, c.DoesRequestSatisfyTermination(r)) |
| 40 | +} |
| 41 | + |
| 42 | +func TestSystemSecret(t *testing.T) { |
| 43 | + c3 := &Config{} |
| 44 | + assert.EqualValues(t, c3.GetSystemSecret(), c3.GetSystemSecret()) |
| 45 | + c := &Config{SystemSecret: "foobarbazbarasdfasdffoobarbazbarasdfasdf"} |
| 46 | + assert.EqualValues(t, c.GetSystemSecret(), c.GetSystemSecret()) |
| 47 | + c2 := &Config{SystemSecret: "foobarbazbarasdfasdffoobarbazbarasdfasdf"} |
| 48 | + assert.EqualValues(t, c.GetSystemSecret(), c2.GetSystemSecret()) |
| 49 | +} |
| 50 | + |
| 51 | +func TestResolve(t *testing.T) { |
| 52 | + c := &Config{ClusterURL: "https://localhost:1234"} |
| 53 | + assert.Equal(t, c.Resolve("foo", "bar").String(), "https://localhost:1234/foo/bar") |
| 54 | + assert.Equal(t, c.Resolve("/foo", "/bar").String(), "https://localhost:1234/foo/bar") |
| 55 | + |
| 56 | + c = &Config{ClusterURL: "https://localhost:1234/"} |
| 57 | + assert.Equal(t, c.Resolve("/foo", "/bar").String(), "https://localhost:1234/foo/bar") |
| 58 | + |
| 59 | + c = &Config{ClusterURL: "https://localhost:1234/bar"} |
| 60 | + assert.Equal(t, c.Resolve("/foo", "/bar").String(), "https://localhost:1234/bar/foo/bar") |
| 61 | +} |
| 62 | + |
| 63 | +func TestLifespan(t *testing.T) { |
| 64 | + assert.Equal(t, (&Config{}).GetAccessTokenLifespan(), time.Hour) |
| 65 | + assert.Equal(t, (&Config{AccessTokenLifespan: "6h"}).GetAccessTokenLifespan(), time.Hour*6) |
| 66 | + |
| 67 | + assert.Equal(t, (&Config{}).GetAuthCodeLifespan(), time.Minute*10) |
| 68 | + assert.Equal(t, (&Config{AuthCodeLifespan: "15m"}).GetAuthCodeLifespan(), time.Minute*15) |
| 69 | + |
| 70 | + assert.Equal(t, (&Config{}).GetIDTokenLifespan(), time.Hour) |
| 71 | + assert.Equal(t, (&Config{IDTokenLifespan: "10s"}).GetIDTokenLifespan(), time.Second*10) |
8 | 72 | } |
0 commit comments