Skip to content

Commit

Permalink
Merge pull request #2 from martinlevesque/test
Browse files Browse the repository at this point in the history
Test
  • Loading branch information
martinlevesque authored Feb 13, 2022
2 parents dcbe8ca + 77e6170 commit a9c37f6
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 1 deletion.
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")
}
}

0 comments on commit a9c37f6

Please sign in to comment.