-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Key changes: - Added some tests; - Moved to Go 1.18, Alpine 3.15.1; - Moved to zerolog: - Pretty formatting by default, JSON is also an option (env: `LOG_FORMAT`: `pretty`, `json`); - Optional access log (env `LOG_REQUESTS`: `true`); - NOTE: Logging format is subject to change.
- Loading branch information
Showing
19 changed files
with
525 additions
and
106 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,6 @@ linters: | |
|
||
linters-settings: | ||
gosimple: | ||
go: "1.17" | ||
go: "1.18" | ||
gocyclo: | ||
min-complexity: 10 |
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
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,151 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/rs/zerolog" | ||
) | ||
|
||
func TestGetRawAccessToken(t *testing.T) { | ||
logger := zerolog.New(nil) | ||
app := &application{ | ||
logger: &logger, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
header string | ||
fail bool | ||
want string | ||
}{ | ||
{ | ||
name: "X-Forwarded-Access-Token", | ||
header: "X-Forwarded-Access-Token", | ||
fail: false, | ||
want: "FAKE_TOKEN", | ||
}, | ||
{ | ||
name: "X-Auth-Request-Access-Token", | ||
header: "X-Auth-Request-Access-Token", | ||
fail: false, | ||
want: "FAKE_TOKEN", | ||
}, | ||
{ | ||
name: "Authorization", | ||
header: "Authorization", | ||
fail: false, | ||
want: "FAKE_TOKEN", | ||
}, | ||
{ | ||
name: "Random header", | ||
header: "Random-Header", | ||
fail: true, | ||
want: "", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
r, err := http.NewRequest(http.MethodGet, "/", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
switch tt.header { | ||
case "Authorization": | ||
r.Header.Set(tt.header, fmt.Sprintf("Bearer %s", tt.want)) | ||
default: | ||
r.Header.Set(tt.header, tt.want) | ||
} | ||
|
||
got, err := app.getRawAccessToken(r) | ||
if tt.fail { | ||
if err == nil { | ||
t.Error("Expected a non-nil error, though got a nil one") | ||
} | ||
} else { | ||
if got != tt.want { | ||
t.Errorf("want %s; got %s", tt.want, got) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestIsUnsafePath(t *testing.T) { | ||
logger := zerolog.New(nil) | ||
app := &application{ | ||
logger: &logger, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
path string | ||
want bool | ||
}{ | ||
{ | ||
name: "tsdb", | ||
path: "/admin/tsdb/1", | ||
want: true, | ||
}, | ||
{ | ||
name: "write", | ||
path: "/api/v1/write", | ||
want: true, | ||
}, | ||
{ | ||
name: "random endpoint", | ||
path: "/api/v1/random", | ||
want: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := app.isUnsafePath(tt.path) | ||
if got != tt.want { | ||
t.Errorf("want %t; got %t", tt.want, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestIsNotAPIRequest(t *testing.T) { | ||
logger := zerolog.New(nil) | ||
app := &application{ | ||
logger: &logger, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
path string | ||
want bool | ||
}{ | ||
{ | ||
name: "api", | ||
path: "/api/v1/query", | ||
want: false, | ||
}, | ||
{ | ||
name: "federate", | ||
path: "/federate", | ||
want: false, | ||
}, | ||
{ | ||
name: "random endpoint", | ||
path: "/metrics", | ||
want: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := app.isNotAPIRequest(tt.path) | ||
if got != tt.want { | ||
t.Errorf("want %t; got %t", tt.want, got) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.