Skip to content

Commit

Permalink
Add unit tests for makeFunctionURL
Browse files Browse the repository at this point in the history
Extracts logic for building URL to makeFunctionURL and adds
several unit tests for either mode and querystring/path
combination.

Signed-off-by: Alex Ellis <alexellis2@gmail.com>
  • Loading branch information
alexellis committed May 7, 2019
1 parent 04af2e8 commit d30e681
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 23 deletions.
50 changes: 27 additions & 23 deletions main.go
Expand Up @@ -21,6 +21,32 @@ import (
"github.com/openfaas/nats-queue-worker/nats"
)

func makeFunctionURL(req *queue.Request, config *QueueWorkerConfig, path, queryString string) string {
qs := ""
if len(queryString) > 0 {
qs = fmt.Sprintf("?%s", strings.TrimLeft(queryString, "?"))
}
pathVal := "/"
if len(path) > 0 {
pathVal = path
}
functionURL := fmt.Sprintf("http://%s%s:8080%s%s",
req.Function,
config.FunctionSuffix,
pathVal,
qs)

if config.GatewayInvoke {
functionURL = fmt.Sprintf("http://%s:8080/function/%s%s%s",
config.GatewayAddress,
strings.Trim(req.Function, "/"),
pathVal,
qs)
}

return functionURL
}

func main() {
readConfig := ReadConfig{}
config := readConfig.Read()
Expand Down Expand Up @@ -67,29 +93,7 @@ func main() {
fmt.Println(string(req.Body))
}

path := "/"
if len(req.Path) > 0 {
path = req.Path
}

queryString := ""
if len(req.QueryString) > 0 {
queryString = fmt.Sprintf("?%s", strings.TrimLeft(req.QueryString, "?"))
}

functionURL := fmt.Sprintf("http://%s%s:8080%s%s",
req.Function,
config.FunctionSuffix,
path,
queryString)

if config.GatewayInvoke {
functionURL = fmt.Sprintf("http://%s:8080/function/%s%s%s",
config.GatewayAddress,
strings.Trim(req.Function, "/"),
path,
queryString)
}
functionURL := makeFunctionURL(&req, &config, req.Path, req.QueryString)

start := time.Now()
request, err := http.NewRequest(http.MethodPost, functionURL, bytes.NewReader(req.Body))
Expand Down
80 changes: 80 additions & 0 deletions main_test.go
@@ -0,0 +1,80 @@
package main

import (
"testing"

"github.com/openfaas/faas/gateway/queue"
)

func Test_makeFunctionURL_DefaultPathQS_GatewayInvoke_IncludesGWAddress(t *testing.T) {
config := QueueWorkerConfig{
FunctionSuffix: "",
GatewayInvoke: true,
GatewayAddress: "gateway",
}
req := queue.Request{
Function: "function1",
Path: "/",
}

fnURL := makeFunctionURL(&req, &config, req.Path, req.QueryString)
wantURL := "http://gateway:8080/function/function1/"
if fnURL != wantURL {
t.Errorf("want %s, got %s", wantURL, fnURL)
}
}

func Test_makeFunctionURL_DefaultPathQS_GatewayInvoke_WithQS(t *testing.T) {
config := QueueWorkerConfig{
FunctionSuffix: "",
GatewayInvoke: true,
GatewayAddress: "gateway",
}
req := queue.Request{
Function: "function1",
QueryString: "user=1",
}

fnURL := makeFunctionURL(&req, &config, req.Path, req.QueryString)
wantURL := "http://gateway:8080/function/function1/?user=1"
if fnURL != wantURL {
t.Errorf("want %s, got %s", wantURL, fnURL)
}
}

func Test_makeFunctionURL_DefaultPathQS_GatewayInvoke_WithPath(t *testing.T) {
config := QueueWorkerConfig{
FunctionSuffix: "",
GatewayInvoke: true,
GatewayAddress: "gateway",
}
req := queue.Request{
Function: "function1",
Path: "/resources/main.css",
}

fnURL := makeFunctionURL(&req, &config, req.Path, req.QueryString)
wantURL := "http://gateway:8080/function/function1/resources/main.css"
if fnURL != wantURL {
t.Errorf("want %s, got %s", wantURL, fnURL)
}
}

func Test_makeFunctionURL_DefaultPathQS_GatewayInvokeOff_UsesDirectInvocation(t *testing.T) {
config := QueueWorkerConfig{
FunctionSuffix: ".openfaas-fn",
GatewayInvoke: false,
GatewayAddress: "gateway",
}
req := queue.Request{
Function: "function1",
Path: "/",
}

fnURL := makeFunctionURL(&req, &config, req.Path, req.QueryString)

wantURL := "http://function1.openfaas-fn:8080/"
if fnURL != wantURL {
t.Errorf("want %s, got %s", wantURL, fnURL)
}
}

0 comments on commit d30e681

Please sign in to comment.