-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from martinlevesque/test
Test
- Loading branch information
Showing
4 changed files
with
175 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
name: Test | ||
|
||
on: | ||
push: | ||
pull_request: | ||
|
||
jobs: | ||
|
||
main: | ||
name: Test middleware | ||
runs-on: ubuntu-latest | ||
env: | ||
GO_VERSION: 1.17 | ||
GOLANGCI_LINT_VERSION: v1.42.1 | ||
YAEGI_VERSION: v0.10.0 | ||
CGO_ENABLED: 0 | ||
defaults: | ||
run: | ||
working-directory: ${{ github.workspace }}/go/src/github.com/${{ github.repository }} | ||
|
||
steps: | ||
|
||
# https://github.com/marketplace/actions/setup-go-environment | ||
- name: Set up Go ${{ env.GO_VERSION }} | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: ${{ env.GO_VERSION }} | ||
|
||
# https://github.com/marketplace/actions/checkout | ||
- name: Check out code | ||
uses: actions/checkout@v2 | ||
with: | ||
path: go/src/github.com/${{ github.repository }} | ||
fetch-depth: 0 | ||
|
||
# https://github.com/marketplace/actions/cache | ||
- name: Cache Go modules | ||
uses: actions/cache@v2 | ||
with: | ||
path: ${{ github.workspace }}/go/pkg/mod | ||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} | ||
restore-keys: | | ||
${{ runner.os }}-go- | ||
- name: Setup GOPATH | ||
run: go env -w GOPATH=${{ github.workspace }}/go | ||
|
||
- name: Check and get dependencies | ||
run: | | ||
go mod tidy | ||
git diff --exit-code go.mod | ||
# git diff --exit-code go.sum | ||
go mod download | ||
go mod vendor | ||
# git diff --exit-code ./vendor/ | ||
- name: Tests | ||
run: make | ||
|
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,16 @@ | ||
|
||
export GO111MODULE=on | ||
|
||
default: test | ||
|
||
test: | ||
go test -v -cover ./... | ||
|
||
yaegi_test: | ||
yaegi test -v . | ||
|
||
vendor: | ||
go mod vendor | ||
|
||
clean: | ||
rm -rf ./vendor |
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,98 @@ | ||
package http_shaping_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/martinlevesque/http_shaping" | ||
) | ||
|
||
func TestHttpShapingHappyPath(t *testing.T) { | ||
cfg := http_shaping.CreateConfig() | ||
cfg.LoopInterval = 5 | ||
cfg.InTrafficLimit = "1KiB" | ||
cfg.OutTrafficLimit = "1KiB" | ||
cfg.ConsiderLimits = true | ||
|
||
ctx := context.Background() | ||
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {}) | ||
|
||
handler, err := http_shaping.New(ctx, next, cfg, "demo-plugin") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
recorder := httptest.NewRecorder() | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
handler.ServeHTTP(recorder, req) | ||
|
||
if recorder.Code != 200 { | ||
t.Errorf("Invalid status code") | ||
} | ||
} | ||
|
||
func TestHttpShapingWithLowLimit(t *testing.T) { | ||
cfg := http_shaping.CreateConfig() | ||
cfg.LoopInterval = 5 | ||
cfg.InTrafficLimit = "0KiB" | ||
cfg.OutTrafficLimit = "1KiB" | ||
cfg.ConsiderLimits = true | ||
|
||
ctx := context.Background() | ||
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {}) | ||
|
||
handler, err := http_shaping.New(ctx, next, cfg, "demo-plugin") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
recorder := httptest.NewRecorder() | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
handler.ServeHTTP(recorder, req) | ||
|
||
if recorder.Code != 429 { | ||
t.Errorf("Invalid status code with low limit") | ||
} | ||
} | ||
|
||
func TestHttpShapingWithLowLimitButShouldIgnore(t *testing.T) { | ||
cfg := http_shaping.CreateConfig() | ||
cfg.LoopInterval = 5 | ||
cfg.InTrafficLimit = "0KiB" | ||
cfg.OutTrafficLimit = "1KiB" | ||
cfg.ConsiderLimits = false | ||
|
||
ctx := context.Background() | ||
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {}) | ||
|
||
handler, err := http_shaping.New(ctx, next, cfg, "demo-plugin") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
recorder := httptest.NewRecorder() | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
handler.ServeHTTP(recorder, req) | ||
|
||
if recorder.Code != 200 { | ||
t.Errorf("Invalid status code") | ||
} | ||
} | ||
|