mirror of
https://github.com/pcvolkmer/idicon.git
synced 2025-04-19 08:36:50 +00:00
Add redirect param to user config
This commit is contained in:
parent
ec33b402b9
commit
80cbf9210d
@ -20,4 +20,5 @@ type UserConfig struct {
|
|||||||
Alias string `toml:"alias"`
|
Alias string `toml:"alias"`
|
||||||
ColorScheme string `toml:"color-scheme"`
|
ColorScheme string `toml:"color-scheme"`
|
||||||
Pattern string `toml:"pattern"`
|
Pattern string `toml:"pattern"`
|
||||||
|
Redirect string `toml:"redirect"`
|
||||||
}
|
}
|
||||||
|
@ -33,6 +33,12 @@ func requestHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
for _, userConfig := range config.Users {
|
for _, userConfig := range config.Users {
|
||||||
if icons.HashBytes(id) == icons.HashBytes(userConfig.ID) {
|
if icons.HashBytes(id) == icons.HashBytes(userConfig.ID) {
|
||||||
|
if userConfig.Redirect != "" {
|
||||||
|
w.Header().Add("Location", userConfig.Redirect)
|
||||||
|
w.WriteHeader(http.StatusFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
id = userConfig.Alias
|
id = userConfig.Alias
|
||||||
if len(userConfig.ColorScheme) > 0 {
|
if len(userConfig.ColorScheme) > 0 {
|
||||||
colorScheme = userConfig.ColorScheme
|
colorScheme = userConfig.ColorScheme
|
||||||
|
@ -141,3 +141,23 @@ func TestCorrectResponseForUserConfig(t *testing.T) {
|
|||||||
t.Errorf("returned image does not match expected image for mapped alias '42'")
|
t.Errorf("returned image does not match expected image for mapped alias '42'")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCorrectRedirect(t *testing.T) {
|
||||||
|
configure("./testdata/testconfig.toml")
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", "/avatar/example2", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
testRouter().ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
if code := rr.Code; code != http.StatusFound {
|
||||||
|
t.Errorf("response code match: got %d want %d", code, http.StatusFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
if location := rr.Header().Get("Location"); location != "https://avatars.example.com/u/42" {
|
||||||
|
t.Errorf("location header does not match: got %v want https://avatars.example.com/u/42", location)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
4
testdata/testconfig.toml
vendored
4
testdata/testconfig.toml
vendored
@ -6,3 +6,7 @@ id = "example"
|
|||||||
alias = "42"
|
alias = "42"
|
||||||
color-scheme = "gh"
|
color-scheme = "gh"
|
||||||
pattern = "github"
|
pattern = "github"
|
||||||
|
|
||||||
|
[[users]]
|
||||||
|
id = "example2"
|
||||||
|
redirect = "https://avatars.example.com/u/42"
|
Loading…
x
Reference in New Issue
Block a user