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

Added support for custom host #159

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 19 additions & 10 deletions apitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type APITest struct {
recorder *Recorder
handler http.Handler
name string
host string
request *Request
response *Response
observers []Observe
Expand Down Expand Up @@ -247,6 +248,12 @@ type pair struct {
r string
}

// Host is a builder method for explicitly setting the host
func (a *APITest) Host(host string) *APITest {
a.host = host
return a
}

// Intercept is a builder method for setting the request interceptor
func (a *APITest) Intercept(interceptor Intercept) *APITest {
a.request.interceptor = interceptor
Expand Down Expand Up @@ -285,10 +292,9 @@ func (a *APITest) Getf(format string, args ...interface{}) *Request {

// Post is a convenience method for setting the request as http.MethodPost
func (a *APITest) Post(url string) *Request {
r := a.request
r.method = http.MethodPost
r.url = url
return r
a.request.method = http.MethodPost
a.request.url = url
return a.request
}

// Postf is a convenience method that adds formatting support to Post
Expand All @@ -298,10 +304,9 @@ func (a *APITest) Postf(format string, args ...interface{}) *Request {

// Put is a convenience method for setting the request as http.MethodPut
func (a *APITest) Put(url string) *Request {
r := a.request
r.method = http.MethodPut
r.url = url
return r
a.request.method = http.MethodPut
a.request.url = url
return a.request
}

// Putf is a convenience method that adds formatting support to Put
Expand Down Expand Up @@ -684,7 +689,7 @@ func (r *Response) End() Result {
defer func() {
if apiTest.debugEnabled {
apiTest.finished = time.Now()
fmt.Println(fmt.Sprintf("Duration: %s\n", apiTest.finished.Sub(apiTest.started)))
fmt.Printf("Duration: %s\n", apiTest.finished.Sub(apiTest.started))
}
}()

Expand All @@ -702,7 +707,7 @@ func (r *Response) End() Result {

var unmatchedMocks []UnmatchedMock
for _, m := range r.apiTest.mocks {
if m.isUsed == false {
if !m.isUsed {
unmatchedMocks = append(unmatchedMocks, UnmatchedMock{
URL: *m.request.url,
})
Expand Down Expand Up @@ -995,6 +1000,10 @@ func (a *APITest) buildRequest() *http.Request {

req.URL.RawQuery = formatQuery(a.request)
req.Host = SystemUnderTestDefaultName
if len(a.host) > 0 {
req.Host = a.host
}

if a.networkingEnabled {
req.Host = req.URL.Host
}
Expand Down
23 changes: 23 additions & 0 deletions apitest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,29 @@ import (
"github.com/steinfletcher/apitest/mocks"
)

func TestApiTest_CustomHost(t *testing.T) {
handler := http.NewServeMux()
handler.Handle("/hello", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
host := strings.Split(r.Host, ".")
subdomain := host[0]

if subdomain != "demo" {
w.WriteHeader(http.StatusBadRequest)
return
}

w.WriteHeader(http.StatusOK)
}))

apitest.New().
Handler(handler).
Host("demo.example.com").
Get("/hello").
Expect(t).
Status(http.StatusOK).
End()
}

func TestApiTest_ResponseBody(t *testing.T) {
apitest.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`{"id": "1234", "name": "Andy"}`))
Expand Down
Loading