-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: dedicate package and tests.
- Loading branch information
Showing
8 changed files
with
230 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package auth | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/containous/traefik/testhelpers" | ||
"github.com/containous/traefik/types" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/urfave/negroni" | ||
) | ||
|
||
func TestForwardAuthFail(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
http.Error(w, "Forbidden", http.StatusForbidden) | ||
})) | ||
defer server.Close() | ||
|
||
middleware, err := NewAuthenticator(&types.Auth{ | ||
Forward: &types.Forward{ | ||
Address: server.URL, | ||
}, | ||
}) | ||
assert.NoError(t, err, "there should be no error") | ||
|
||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprintln(w, "traefik") | ||
}) | ||
n := negroni.New(middleware) | ||
n.UseHandler(handler) | ||
ts := httptest.NewServer(n) | ||
defer ts.Close() | ||
|
||
client := &http.Client{} | ||
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil) | ||
res, err := client.Do(req) | ||
assert.NoError(t, err, "there should be no error") | ||
assert.Equal(t, http.StatusForbidden, res.StatusCode, "they should be equal") | ||
|
||
body, err := ioutil.ReadAll(res.Body) | ||
assert.NoError(t, err, "there should be no error") | ||
assert.Equal(t, "Forbidden\n", string(body), "they should be equal") | ||
} | ||
|
||
func TestForwardAuthSuccess(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprintln(w, "Success") | ||
})) | ||
defer server.Close() | ||
|
||
middleware, err := NewAuthenticator(&types.Auth{ | ||
Forward: &types.Forward{ | ||
Address: server.URL, | ||
}, | ||
}) | ||
assert.NoError(t, err, "there should be no error") | ||
|
||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprintln(w, "traefik") | ||
}) | ||
n := negroni.New(middleware) | ||
n.UseHandler(handler) | ||
ts := httptest.NewServer(n) | ||
defer ts.Close() | ||
|
||
client := &http.Client{} | ||
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil) | ||
res, err := client.Do(req) | ||
assert.NoError(t, err, "there should be no error") | ||
assert.Equal(t, http.StatusOK, res.StatusCode, "they should be equal") | ||
|
||
body, err := ioutil.ReadAll(res.Body) | ||
assert.NoError(t, err, "there should be no error") | ||
assert.Equal(t, "traefik\n", string(body), "they should be equal") | ||
} | ||
|
||
func Test_writeHeader(t *testing.T) { | ||
|
||
testCases := []struct { | ||
name string | ||
headers map[string]string | ||
trustForwardHeader bool | ||
emptyHost bool | ||
expectedHeaders map[string]string | ||
}{ | ||
{ | ||
name: "trust Forward Header", | ||
headers: map[string]string{ | ||
"Accept": "application/json", | ||
"X-Forwarded-Host": "fii.bir", | ||
}, | ||
trustForwardHeader: true, | ||
expectedHeaders: map[string]string{ | ||
"Accept": "application/json", | ||
"X-Forwarded-Host": "fii.bir", | ||
}, | ||
}, | ||
{ | ||
name: "not trust Forward Header", | ||
headers: map[string]string{ | ||
"Accept": "application/json", | ||
"X-Forwarded-Host": "fii.bir", | ||
}, | ||
trustForwardHeader: false, | ||
expectedHeaders: map[string]string{ | ||
"Accept": "application/json", | ||
"X-Forwarded-Host": "foo.bar", | ||
}, | ||
}, | ||
{ | ||
name: "trust Forward Header with empty Host", | ||
headers: map[string]string{ | ||
"Accept": "application/json", | ||
"X-Forwarded-Host": "fii.bir", | ||
}, | ||
trustForwardHeader: true, | ||
emptyHost: true, | ||
expectedHeaders: map[string]string{ | ||
"Accept": "application/json", | ||
"X-Forwarded-Host": "fii.bir", | ||
}, | ||
}, | ||
{ | ||
name: "not trust Forward Header with empty Host", | ||
headers: map[string]string{ | ||
"Accept": "application/json", | ||
"X-Forwarded-Host": "fii.bir", | ||
}, | ||
trustForwardHeader: false, | ||
emptyHost: true, | ||
expectedHeaders: map[string]string{ | ||
"Accept": "application/json", | ||
"X-Forwarded-Host": "", | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range testCases { | ||
t.Run(test.name, func(t *testing.T) { | ||
|
||
req := testhelpers.MustNewRequest(http.MethodGet, "http://foo.bar", nil) | ||
for key, value := range test.headers { | ||
req.Header.Set(key, value) | ||
} | ||
|
||
if test.emptyHost { | ||
req.Host = "" | ||
} | ||
|
||
forwardReq := testhelpers.MustNewRequest(http.MethodGet, "http://foo.bar", nil) | ||
|
||
writeHeader(req, forwardReq, test.trustForwardHeader) | ||
|
||
for key, value := range test.expectedHeaders { | ||
assert.Equal(t, value, forwardReq.Header.Get(key)) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.