diff --git a/cmd/docgen/docs.go b/cmd/docgen/docs.go index ed576370d..b2769d0be 100644 --- a/cmd/docgen/docs.go +++ b/cmd/docgen/docs.go @@ -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 @@ -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) } @@ -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) } diff --git a/cmd/docgen/handlers.go b/cmd/docgen/handlers.go index 3d679cb51..6c46c2bfb 100644 --- a/cmd/docgen/handlers.go +++ b/cmd/docgen/handlers.go @@ -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 } diff --git a/cmd/flowrunner/runner_test.go b/cmd/flowrunner/runner_test.go index 32fd2d0f8..4f4597e36 100644 --- a/cmd/flowrunner/runner_test.go +++ b/cmd/flowrunner/runner_test.go @@ -18,6 +18,8 @@ import ( "github.com/stretchr/testify/require" ) +var testServerPort = 49999 + var flowTests = []struct { assets string output string @@ -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() diff --git a/docs/docs.md b/docs/docs.md index 0103dec61..3c794b0c9 100644 --- a/docs/docs.md +++ b/docs/docs.md @@ -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" } @@ -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\" }" } ``` diff --git a/docs/index.html b/docs/index.html index bbae37217..3ff9ffe7b 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1221,7 +1221,7 @@

"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" } @@ -1235,10 +1235,10 @@

"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\" }" } diff --git a/flows/actions/call_webhook.go b/flows/actions/call_webhook.go index 47b09a04f..f39eb500a 100644 --- a/flows/actions/call_webhook.go +++ b/flows/actions/call_webhook.go @@ -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" // } diff --git a/flows/engine/session_test.go b/flows/engine/session_test.go index c88533ea2..dcd6f4d33 100644 --- a/flows/engine/session_test.go +++ b/flows/engine/session_test.go @@ -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) } diff --git a/legacy/expressions_test.go b/legacy/expressions_test.go index 5c55447b0..160e3d25b 100644 --- a/legacy/expressions_test.go +++ b/legacy/expressions_test.go @@ -9,6 +9,8 @@ import ( "github.com/stretchr/testify/require" ) +var testServerPort = 49997 + type testTemplate struct { old string new string @@ -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 { diff --git a/test/http.go b/test/http.go index 0cddcfee0..f818cd03e 100644 --- a/test/http.go +++ b/test/http.go @@ -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 { @@ -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 } diff --git a/test/session.go b/test/session.go index 791c2ab27..232812a9e 100644 --- a/test/session.go +++ b/test/session.go @@ -3,6 +3,7 @@ package test import ( "encoding/json" "fmt" + "strings" "time" "github.com/nyaruka/goflow/flows" @@ -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": [ @@ -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 {