Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test #2

Merged
merged 3 commits into from
Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions .github/workflows/main.yml
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

16 changes: 16 additions & 0 deletions Makefile
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
3 changes: 2 additions & 1 deletion http_shaping.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package http_shaping

import (
"bytes"
"context"
"fmt"
"net/http"
Expand Down Expand Up @@ -128,6 +127,8 @@ func (a *HttpShaping) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}
}

// the following bytes conversion methods are taken from:
// https://github.com/cloudfoundry/bytefmt/blob/master/bytes.go

const (
BYTE = 1 << (10 * iota)
Expand Down
98 changes: 98 additions & 0 deletions http_shaping_test.go
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")
}
}