Skip to content

Commit

Permalink
fix: avoid some nil refs and add verbose mode to individual requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Sheldon committed Sep 29, 2023
1 parent 15d9d51 commit 98c237e
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 13 deletions.
Binary file modified dist/windows/amd64/nap.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/routines/basic/request-1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ kind: request
name: Cat Breeds - Assertion/Capture Testing
path: https://catfact.ninja/breeds
postRequestScript: |
console.log('Hello World!');
nap.env.set("thirdBreed", nap.env.get("firstBreed"))
asserts:
- status == 200
- duration < 1000
Expand Down
4 changes: 1 addition & 3 deletions examples/routines/basic/routine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ kind: routine
name: main routine
steps:
- run: request-1.yml
- run: request-2.yml
- run: subroutine-1.yml
- run: subroutine-2.yml
- run: script.js
4 changes: 1 addition & 3 deletions examples/routines/basic/script.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
console.log("my status code: " + nap.env.get("myStatus"))
console.log("my duration: " + nap.env.get("myDuration"))
console.log("Here's a second console.log")
console.log("thirdBreed: " + nap.env.get("thirdBreed"))
9 changes: 9 additions & 0 deletions napquery/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ import (
)

func Eval(query string, vmData *napscript.VmHttpData) (string, error) {
if vmData == nil || vmData.Response == nil {
// return empty here instead of erroring in case this assert is testing for absence of a value
return "", nil
}

jsonExpression, isJsonPath := strings.CutPrefix(query, "jsonpath ")
if isJsonPath {
body := vmData.Response.JsonBody
Expand All @@ -42,6 +47,10 @@ func Eval(query string, vmData *napscript.VmHttpData) (string, error) {

header, isHeader := strings.CutPrefix(query, "header ")
if isHeader {
if vmData.Response.Headers == nil {
return "", nil
}

value := vmData.Response.Headers[header]

return strings.Join(value, ","), nil
Expand Down
1 change: 1 addition & 0 deletions naprequest/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Request struct {
TimeoutSeconds int `yaml:"timeoutSeconds"`
Captures map[string]string
Asserts []string
Verbose bool
}

func parse(data []byte) (*Request, error) {
Expand Down
30 changes: 24 additions & 6 deletions naprunner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"io"
"net/http"
"net/http/httputil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -102,10 +103,6 @@ func runRequest(ctx *napcontext.Context, request *naprequest.Request) *napreques

result.HttpResponse, result.Error = executeHttp(request)

if result.HttpResponse != nil && result.HttpResponse.StatusCode >= 400 {
result.Error = errors.New(fmt.Sprintf("HTTP %s", result.HttpResponse.Status))
}

result.EndTime = time.Now()

vmData, err := napscript.SetVmHttpData(ctx, result)
Expand Down Expand Up @@ -187,13 +184,34 @@ func executeHttp(r *naprequest.Request) (*http.Response, error) {
request.Header.Add(k, v)
}

resp, err := client.Do(request)
response, err := client.Do(request)

if r.Verbose {
fmt.Println("REQUEST:")
dump, err := httputil.DumpRequestOut(request, true)
if err == nil {

fmt.Println(string(dump))
} else {
fmt.Println(err)
}
}

if r.Verbose {
fmt.Println("RESPONSE:")
dump, err := httputil.DumpResponse(response, true)
if err == nil {
fmt.Println(string(dump))
} else {
fmt.Println(err)
}
}

if err != nil {
return nil, err
}

return resp, nil
return response, nil
}

func fileExists(path string) bool {
Expand Down

0 comments on commit 98c237e

Please sign in to comment.