Skip to content

Commit

Permalink
Merge pull request #249 from nyaruka/test_server_fix
Browse files Browse the repository at this point in the history
Fix test HTTP server
  • Loading branch information
rowanseymour authored Apr 17, 2018
2 parents 967bd6b + c6fa6ea commit 7957e34
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 18 deletions.
6 changes: 4 additions & 2 deletions cmd/docgen/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/nyaruka/goflow/utils"
)

var testServerPort = 49998

type documentedItem struct {
typeName string // actual go type name
tagName string // tag used to make this as a documented item
Expand All @@ -30,7 +32,7 @@ type handleFunc func(output *strings.Builder, item *documentedItem, session flow
func buildDocs(baseDir string) (string, error) {
fmt.Println("Generating docs...")

server, err := test.NewTestHTTPServer()
server, err := test.NewTestHTTPServer(testServerPort)
if err != nil {
return "", fmt.Errorf("error starting mock HTTP server: %s", err)
}
Expand All @@ -42,7 +44,7 @@ func buildDocs(baseDir string) (string, error) {
utils.SetUUIDGenerator(utils.NewSeededUUID4Generator(123456))
defer utils.SetUUIDGenerator(utils.DefaultUUIDGenerator)

session, err := test.CreateTestSession(nil)
session, err := test.CreateTestSession(testServerPort, nil)
if err != nil {
return "", fmt.Errorf("error creating example session: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/docgen/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func checkExample(session flows.Session, line string) error {
}

func eventsForAction(action flows.Action) (json.RawMessage, error) {
session, err := test.CreateTestSession(action)
session, err := test.CreateTestSession(49998, action)
if err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/flowrunner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"github.com/stretchr/testify/require"
)

var testServerPort = 49999

var flowTests = []struct {
assets string
output string
Expand Down Expand Up @@ -141,7 +143,7 @@ func runFlow(assetsFilename string, triggerEnvelope *utils.TypedEnvelope, caller
}

func TestFlows(t *testing.T) {
server, err := test.NewTestHTTPServer()
server, err := test.NewTestHTTPServer(testServerPort)
require.NoError(t, err)

defer server.Close()
Expand Down
6 changes: 3 additions & 3 deletions docs/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1829,7 +1829,7 @@ A `webhook_called` event will be created based on the results of the HTTP call.
"type": "call_webhook",
"uuid": "8eebd020-1af5-431c-b943-aa670fc74da9",
"method": "GET",
"url": "http://localhost:49999/?cmd=success",
"url": "http://localhost:49998/?cmd=success",
"headers": {
"Authorization": "Token AAFFZZHH"
}
Expand All @@ -1840,10 +1840,10 @@ A `webhook_called` event will be created based on the results of the HTTP call.
"type": "webhook_called",
"created_on": "2018-04-11T13:24:30.123456-05:00",
"step_uuid": "229bd432-dac7-4a3f-ba91-c48ad8c50e6b",
"url": "http://localhost:49999/?cmd=success",
"url": "http://localhost:49998/?cmd=success",
"status": "success",
"status_code": 200,
"request": "GET /?cmd=success HTTP/1.1\r\nHost: localhost:49999\r\nUser-Agent: Go-http-client/1.1\r\nAuthorization: Token AAFFZZHH\r\nAccept-Encoding: gzip\r\n\r\n",
"request": "GET /?cmd=success HTTP/1.1\r\nHost: localhost:49998\r\nUser-Agent: Go-http-client/1.1\r\nAuthorization: Token AAFFZZHH\r\nAccept-Encoding: gzip\r\n\r\n",
"response": "HTTP/1.1 200 OK\r\nContent-Length: 16\r\nContent-Type: text/plain; charset=utf-8\r\nDate: Wed, 11 Apr 2018 18:24:30 GMT\r\n\r\n{ \"ok\": \"true\" }"
}
```
Expand Down
6 changes: 3 additions & 3 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,7 @@ <h3>
<a class="sourceLine" id="cb107-2" data-line-number="2"> <span class="dt">&quot;type&quot;</span><span class="fu">:</span> <span class="st">&quot;call_webhook&quot;</span><span class="fu">,</span></a>
<a class="sourceLine" id="cb107-3" data-line-number="3"> <span class="dt">&quot;uuid&quot;</span><span class="fu">:</span> <span class="st">&quot;8eebd020-1af5-431c-b943-aa670fc74da9&quot;</span><span class="fu">,</span></a>
<a class="sourceLine" id="cb107-4" data-line-number="4"> <span class="dt">&quot;method&quot;</span><span class="fu">:</span> <span class="st">&quot;GET&quot;</span><span class="fu">,</span></a>
<a class="sourceLine" id="cb107-5" data-line-number="5"> <span class="dt">&quot;url&quot;</span><span class="fu">:</span> <span class="st">&quot;http://localhost:49999/?cmd=success&quot;</span><span class="fu">,</span></a>
<a class="sourceLine" id="cb107-5" data-line-number="5"> <span class="dt">&quot;url&quot;</span><span class="fu">:</span> <span class="st">&quot;http://localhost:49998/?cmd=success&quot;</span><span class="fu">,</span></a>
<a class="sourceLine" id="cb107-6" data-line-number="6"> <span class="dt">&quot;headers&quot;</span><span class="fu">:</span> <span class="fu">{</span></a>
<a class="sourceLine" id="cb107-7" data-line-number="7"> <span class="dt">&quot;Authorization&quot;</span><span class="fu">:</span> <span class="st">&quot;Token AAFFZZHH&quot;</span></a>
<a class="sourceLine" id="cb107-8" data-line-number="8"> <span class="fu">}</span></a>
Expand All @@ -1235,10 +1235,10 @@ <h3>
<a class="sourceLine" id="cb108-2" data-line-number="2"> <span class="dt">&quot;type&quot;</span><span class="fu">:</span> <span class="st">&quot;webhook_called&quot;</span><span class="fu">,</span></a>
<a class="sourceLine" id="cb108-3" data-line-number="3"> <span class="dt">&quot;created_on&quot;</span><span class="fu">:</span> <span class="st">&quot;2018-04-11T13:24:30.123456-05:00&quot;</span><span class="fu">,</span></a>
<a class="sourceLine" id="cb108-4" data-line-number="4"> <span class="dt">&quot;step_uuid&quot;</span><span class="fu">:</span> <span class="st">&quot;229bd432-dac7-4a3f-ba91-c48ad8c50e6b&quot;</span><span class="fu">,</span></a>
<a class="sourceLine" id="cb108-5" data-line-number="5"> <span class="dt">&quot;url&quot;</span><span class="fu">:</span> <span class="st">&quot;http://localhost:49999/?cmd=success&quot;</span><span class="fu">,</span></a>
<a class="sourceLine" id="cb108-5" data-line-number="5"> <span class="dt">&quot;url&quot;</span><span class="fu">:</span> <span class="st">&quot;http://localhost:49998/?cmd=success&quot;</span><span class="fu">,</span></a>
<a class="sourceLine" id="cb108-6" data-line-number="6"> <span class="dt">&quot;status&quot;</span><span class="fu">:</span> <span class="st">&quot;success&quot;</span><span class="fu">,</span></a>
<a class="sourceLine" id="cb108-7" data-line-number="7"> <span class="dt">&quot;status_code&quot;</span><span class="fu">:</span> <span class="dv">200</span><span class="fu">,</span></a>
<a class="sourceLine" id="cb108-8" data-line-number="8"> <span class="dt">&quot;request&quot;</span><span class="fu">:</span> <span class="st">&quot;GET /?cmd=success HTTP/1.1</span><span class="ch">\r\n</span><span class="st">Host: localhost:49999</span><span class="ch">\r\n</span><span class="st">User-Agent: Go-http-client/1.1</span><span class="ch">\r\n</span><span class="st">Authorization: Token AAFFZZHH</span><span class="ch">\r\n</span><span class="st">Accept-Encoding: gzip</span><span class="ch">\r\n\r\n</span><span class="st">&quot;</span><span class="fu">,</span></a>
<a class="sourceLine" id="cb108-8" data-line-number="8"> <span class="dt">&quot;request&quot;</span><span class="fu">:</span> <span class="st">&quot;GET /?cmd=success HTTP/1.1</span><span class="ch">\r\n</span><span class="st">Host: localhost:49998</span><span class="ch">\r\n</span><span class="st">User-Agent: Go-http-client/1.1</span><span class="ch">\r\n</span><span class="st">Authorization: Token AAFFZZHH</span><span class="ch">\r\n</span><span class="st">Accept-Encoding: gzip</span><span class="ch">\r\n\r\n</span><span class="st">&quot;</span><span class="fu">,</span></a>
<a class="sourceLine" id="cb108-9" data-line-number="9"> <span class="dt">&quot;response&quot;</span><span class="fu">:</span> <span class="st">&quot;HTTP/1.1 200 OK</span><span class="ch">\r\n</span><span class="st">Content-Length: 16</span><span class="ch">\r\n</span><span class="st">Content-Type: text/plain; charset=utf-8</span><span class="ch">\r\n</span><span class="st">Date: Wed, 11 Apr 2018 18:24:30 GMT</span><span class="ch">\r\n\r\n</span><span class="st">{ </span><span class="ch">\&quot;</span><span class="st">ok</span><span class="ch">\&quot;</span><span class="st">: </span><span class="ch">\&quot;</span><span class="st">true</span><span class="ch">\&quot;</span><span class="st"> }&quot;</span></a>
<a class="sourceLine" id="cb108-10" data-line-number="10"><span class="fu">}</span></a></code></pre></div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion flows/actions/call_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const TypeCallWebhook string = "call_webhook"
// "uuid": "8eebd020-1af5-431c-b943-aa670fc74da9",
// "type": "call_webhook",
// "method": "GET",
// "url": "http://localhost:49999/?cmd=success",
// "url": "http://localhost:49998/?cmd=success",
// "headers": {
// "Authorization": "Token AAFFZZHH"
// }
Expand Down
1 change: 0 additions & 1 deletion flows/engine/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ func createTestSession(t *testing.T) flows.FlowRun {

// check for unexpected errors in the session
for _, event := range session.Events() {
fmt.Printf("event %s\n", event.Type())
require.NotEqual(t, event.Type(), events.TypeError)
}

Expand Down
6 changes: 4 additions & 2 deletions legacy/expressions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/stretchr/testify/require"
)

var testServerPort = 49997

type testTemplate struct {
old string
new string
Expand Down Expand Up @@ -191,12 +193,12 @@ func TestMigrateTemplate(t *testing.T) {
tests = append(tests, testTemplate{old: "Replace " + tests[i].old + " two " + tests[i].old + " times", new: "Replace " + tests[i].new + " two " + tests[i].new + " times", extraAs: tests[i].extraAs})
}

server, err := test.NewTestHTTPServer()
server, err := test.NewTestHTTPServer(testServerPort)
require.NoError(t, err)

defer server.Close()

session, err := test.CreateTestSession(nil)
session, err := test.CreateTestSession(testServerPort, nil)
require.NoError(t, err)

for _, test := range tests {
Expand Down
6 changes: 4 additions & 2 deletions test/http.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package test

import (
"fmt"
"net"
"net/http"
"net/http/httptest"
)

// NewTestHTTPServer sets up a mock server for webhook actions
func NewTestHTTPServer() (*httptest.Server, error) {
func NewTestHTTPServer(port int) (*httptest.Server, error) {
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cmd := r.URL.Query().Get("cmd")
defer r.Body.Close()

w.Header().Set("Date", "Wed, 11 Apr 2018 18:24:30 GMT")

switch cmd {
Expand All @@ -29,7 +31,7 @@ func NewTestHTTPServer() (*httptest.Server, error) {
}
}))
// manually create a listener for our test server so that our output is predictable
l, err := net.Listen("tcp", "127.0.0.1:49999")
l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
if err != nil {
return nil, err
}
Expand Down
8 changes: 6 additions & 2 deletions test/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package test
import (
"encoding/json"
"fmt"
"strings"
"time"

"github.com/nyaruka/goflow/flows"
Expand Down Expand Up @@ -87,7 +88,7 @@ var sessionAssets = `[
"uuid": "06153fbd-3e2c-413a-b0df-ed15d631835a",
"type": "call_webhook",
"method": "GET",
"url": "http://localhost:49999/?cmd=echo&content=%7B%22results%22%3A%5B%7B%22state%22%3A%22WA%22%7D%2C%7B%22state%22%3A%22IN%22%7D%5D%7D"
"url": "http://localhost:TEST_SERVER_PORT/?cmd=echo&content=%7B%22results%22%3A%5B%7B%22state%22%3A%22WA%22%7D%2C%7B%22state%22%3A%22IN%22%7D%5D%7D"
}
],
"exits": [
Expand Down Expand Up @@ -315,7 +316,10 @@ func (e *testEnvironment) Now() time.Time {
}

// CreateTestSession creates an example session for testing
func CreateTestSession(actionToAdd flows.Action) (flows.Session, error) {
func CreateTestSession(testServerPort int, actionToAdd flows.Action) (flows.Session, error) {
// different tests different ports for the test HTTP server
sessionAssets = strings.Replace(sessionAssets, "TEST_SERVER_PORT", fmt.Sprintf("%d", testServerPort), -1)

// read our assets
assetCache := engine.NewAssetCache(100, 5, "testing/1.0")
if err := assetCache.Include(json.RawMessage(sessionAssets)); err != nil {
Expand Down

0 comments on commit 7957e34

Please sign in to comment.