| 1 |
package op |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/http" |
| 5 |
"net/http/httptest" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/stretchr/testify/assert" |
| 9 |
) |
| 10 |
|
| 11 |
func TestIssuerInterceptor(t *testing.T) { |
| 12 |
type fields struct { |
| 13 |
issuerFromRequest IssuerFromRequest |
| 14 |
} |
| 15 |
type args struct { |
| 16 |
r *http.Request |
| 17 |
next http.Handler |
| 18 |
} |
| 19 |
type res struct { |
| 20 |
issuer string |
| 21 |
} |
| 22 |
tests := []struct { |
| 23 |
name string |
| 24 |
fields fields |
| 25 |
args args |
| 26 |
res res |
| 27 |
}{ |
| 28 |
{ |
| 29 |
"empty", |
| 30 |
fields{ |
| 31 |
func(r *http.Request) string { |
| 32 |
return "" |
| 33 |
}, |
| 34 |
}, |
| 35 |
args{}, |
| 36 |
res{ |
| 37 |
issuer: "", |
| 38 |
}, |
| 39 |
}, |
| 40 |
{ |
| 41 |
"static", |
| 42 |
fields{ |
| 43 |
func(r *http.Request) string { |
| 44 |
return "static" |
| 45 |
}, |
| 46 |
}, |
| 47 |
args{}, |
| 48 |
res{ |
| 49 |
issuer: "static", |
| 50 |
}, |
| 51 |
}, |
| 52 |
{ |
| 53 |
"host", |
| 54 |
fields{ |
| 55 |
func(r *http.Request) string { |
| 56 |
return r.Host |
| 57 |
}, |
| 58 |
}, |
| 59 |
args{}, |
| 60 |
res{ |
| 61 |
issuer: "issuer.com", |
| 62 |
}, |
| 63 |
}, |
| 64 |
} |
| 65 |
for _, tt := range tests { |
| 66 |
t.Run(tt.name, func(t *testing.T) { |
| 67 |
i := NewIssuerInterceptor(tt.fields.issuerFromRequest) |
| 68 |
next := http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { |
| 69 |
assert.Equal(t, tt.res.issuer, IssuerFromContext(r.Context())) |
| 70 |
}) |
| 71 |
req := httptest.NewRequest("", "https://issuer.com", nil) |
| 72 |
i.Handler(next).ServeHTTP(nil, req) |
| 73 |
i.HandlerFunc(next).ServeHTTP(nil, req) |
| 74 |
}) |
| 75 |
} |
| 76 |
} |