Skip to content

Commit

Permalink
Add test for proxy
Browse files Browse the repository at this point in the history
Signed-off-by: Jeroen van Erp <jeroen@hierynomus.com>
  • Loading branch information
hierynomus committed Aug 27, 2021
1 parent c05a043 commit 8636a63
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
6 changes: 5 additions & 1 deletion internal/mirror/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ func (m *Mirror) Reflect(req *Request) {
m.breaker.Execute(func() (interface{}, error) { //nolint:errcheck
url := fmt.Sprintf("%s%s", m.targetURL, req.originalRequest.RequestURI)

newRequest, _ := http.NewRequest(req.originalRequest.Method, url, bytes.NewReader(req.body)) //nolint:noctx
newRequest, err := http.NewRequest(req.originalRequest.Method, url, bytes.NewReader(req.body)) //nolint:noctx
if err != nil {
return nil, err
}

newRequest.Header = req.originalRequest.Header

response, err := m.netClient.Do(newRequest)
Expand Down
57 changes: 57 additions & 0 deletions internal/proxy/proxy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package proxy

import (
"context"
"net/http"
"sync"
"testing"
"time"

"github.com/gin-gonic/gin"
"github.com/rb3ckers/trafficmirror/internal/config"
"github.com/stretchr/testify/assert"
)

func TestReflector(t *testing.T) {
reqs1, reqs2 := 0, 0
wg := &sync.WaitGroup{}
wg.Add(2)
serv1 := gin.New()
serv2 := gin.New()

serv1.GET("/", func(c *gin.Context) {
reqs1++
wg.Done()
c.String(200, "Hello World")
})

serv2.GET("/", func(c *gin.Context) {
reqs2++
wg.Done()
c.String(200, "Hello World")
})

go gin.Default().Run(":8888")
go serv1.Run(":8081")
go serv2.Run(":8082")

p := NewProxy(config.Default())
assert.NoError(t, p.Start(context.Background()))
p.reflector.AddMirrors([]string{"http://localhost:8081", "http://localhost:8082"})

req, err := http.NewRequest("GET", "http://localhost:8080/", nil)
assert.NoError(t, err)

c := &http.Client{
Timeout: time.Second * 20,
}

resp, err := c.Do(req)
assert.NoError(t, err)
resp.Body.Close()

wg.Wait()

assert.Equal(t, 1, reqs1)
assert.Equal(t, 1, reqs2)
}

0 comments on commit 8636a63

Please sign in to comment.