Skip to content

Commit

Permalink
Moved fake server logic to the test
Browse files Browse the repository at this point in the history
  • Loading branch information
Albert Callarisa committed Jul 22, 2022
1 parent 03acff2 commit f389c1e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
19 changes: 19 additions & 0 deletions test/integration_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package test

import (
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"strconv"
"testing"
"time"

Expand All @@ -13,6 +16,22 @@ func TestHappyPath(t *testing.T) {
suite := newSuite(t)
defer suite.teardown()

suite.runFakeServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
number := 0

currentCookie, err := r.Cookie("test-cookie")
if err == http.ErrNoCookie {
number = rand.Intn(10000000)
} else {
number, err = strconv.Atoi(currentCookie.Value)
require.NoError(t, err)
}
w.Header().Add("Set-Cookie", fmt.Sprintf("test-cookie=%v,", number))

w.WriteHeader(200)
w.Write([]byte(strconv.Itoa(number)))
}))

repo := suite.repo()

require.NoError(t, repo.CreateBucket("testBucket"))
Expand Down
21 changes: 2 additions & 19 deletions test/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package test

import (
"fmt"
"math/rand"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"strconv"
"strings"
"testing"

Expand Down Expand Up @@ -37,7 +35,6 @@ func newSuite(t *testing.T) *Suite {
suite.network()
suite.runVault()
suite.runMitm()
suite.runFakeServer()
return suite
}

Expand Down Expand Up @@ -103,7 +100,7 @@ func (s *Suite) runMitm() {
}
}

func (s *Suite) runFakeServer() {
func (s *Suite) runFakeServer(fn http.HandlerFunc) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(s.t, err)
port := listener.Addr().(*net.TCPAddr).Port
Expand All @@ -114,21 +111,7 @@ func (s *Suite) runFakeServer() {
s.fakeServerAddr = fmt.Sprintf("http://fake-server:%v", port)
}

go http.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
number := 0

currentCookie, err := r.Cookie("test-cookie")
if err == http.ErrNoCookie {
number = rand.Intn(10000000)
} else {
number, err = strconv.Atoi(currentCookie.Value)
require.NoError(s.t, err)
}
w.Header().Add("Set-Cookie", fmt.Sprintf("test-cookie=%v,", number))

w.WriteHeader(200)
w.Write([]byte(strconv.Itoa(number)))
}))
go http.Serve(listener, fn)
}

func (s *Suite) cmd(t *testing.T, c string) string {
Expand Down

0 comments on commit f389c1e

Please sign in to comment.