Skip to content

Commit

Permalink
Add req.json_body test #44
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Ludwig committed Oct 20, 2020
1 parent 6be2cd2 commit 3e33faf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
53 changes: 38 additions & 15 deletions eval/context_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eval

import (
"bytes"
"context"
"io"
"net/http"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/zclconf/go-cty/cty"

"github.com/avenga/couper/config/request"
"github.com/avenga/couper/internal/seetie"
)

func TestNewHTTPContext(t *testing.T) {
Expand All @@ -28,41 +30,56 @@ func TestNewHTTPContext(t *testing.T) {
}

type expMap map[string]string
type header map[string]string

baseCtx := NewENVContext(nil)

tests := []struct {
name string
reqMethod string
reqHeader header
body io.Reader
query string
baseCtx *hcl.EvalContext
hcl string
want expMap
}{
{"Variables / POST", http.MethodPost, strings.NewReader(`user=hans`), "", baseCtx, `
post = req.post.user[0]
{"Variables / POST", http.MethodPost, header{"Content-Type": "application/x-www-form-urlencoded"}, strings.NewReader(`user=hans`), "", baseCtx, `
post = req.post.user[0]
method = req.method
`, expMap{"post": "hans", "method": http.MethodPost}},
{"Variables / Query", http.MethodGet, header{"User-Agent": "test/v1"}, nil, "?name=peter", baseCtx, `
query = req.query.name[0]
method = req.method
ua = req.headers.user-agent
`, expMap{"query": "peter", "method": http.MethodGet, "ua": "test/v1"}},
{"Variables / Headers", http.MethodGet, header{"User-Agent": "test/v1"}, nil, "", baseCtx, `
ua = req.headers.user-agent
method = req.method
`, expMap{"ua": "test/v1", "method": http.MethodGet}},
{"Variables / PATCH /w json body", http.MethodPatch, header{"Content-Type": "application/json;charset=utf-8"}, bytes.NewBufferString(`{
"obj_slice": [
{"no_title": 123},
{"title": "success"}
]
}`), "", baseCtx, `
method = req.method
`, expMap{"post": "hans", "method": http.MethodPost}},
{"Variables / Query", http.MethodGet, nil, "?name=peter", baseCtx, `
query = req.query.name[0]
title = req.json_body.obj_slice[1].title
`, expMap{"title": "success", "method": http.MethodPatch}},
{"Variables / PATCH /w json body /wo CT header", http.MethodPatch, nil, bytes.NewBufferString(`{"slice": [1, 2, 3]}`), "", baseCtx, `
method = req.method
ua = req.headers.user-agent
`, expMap{"query": "peter", "method": http.MethodGet, "ua": "test/v1"}},
{"Variables / Headers", http.MethodGet, nil, "", baseCtx, `
ua = req.headers.user-agent
method = req.method
`, expMap{"ua": "test/v1", "method": http.MethodGet}},
title = req.json_body.obj_slice
`, expMap{"title": "", "method": http.MethodPatch}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest(tt.reqMethod, "https://couper.io/"+tt.query, tt.body)
*req = *req.Clone(context.WithValue(req.Context(), request.Endpoint, "couper-proxy"))
if tt.body != nil {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

for k, v := range tt.reqHeader {
req.Header.Set(k, v)
}
req.Header.Set("User-Agent", "test/v1")

bereq := req.Clone(context.Background())
beresp := newBeresp(bereq)
Expand All @@ -72,7 +89,8 @@ func TestNewHTTPContext(t *testing.T) {

var resultMap map[string]cty.Value
err := hclsimple.Decode("test.hcl", []byte(tt.hcl), ctx, &resultMap)
if err != nil {
// Expect same behaviour as in proxy impl and downgrade missing map elements to warnings.
if err != nil && seetie.SetSeverityLevel(err.(hcl.Diagnostics)).HasErrors() {
t.Fatal(err)
}

Expand All @@ -83,6 +101,11 @@ func TestNewHTTPContext(t *testing.T) {
}

cvt := cv.Type()

if cvt != cty.String && v == "" { // expected nothing, go ahead
continue
}

if cvt != cty.String {
t.Fatalf("Expected string value for %q, got %v", k, cvt)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/seetie/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var validKey = regexp.MustCompile("[a-zA-Z_][a-zA-Z0-9_-]*")

func ExpToMap(ctx *hcl.EvalContext, exp hcl.Expression) (map[string]interface{}, hcl.Diagnostics) {
val, diags := exp.Value(ctx)
if setSeverityLevel(diags).HasErrors() {
if SetSeverityLevel(diags).HasErrors() {
return nil, filterErrors(diags)
}
result := make(map[string]interface{})
Expand Down Expand Up @@ -86,7 +86,7 @@ func GoToValue(v interface{}) cty.Value {

func MapToValue(m map[string]interface{}) cty.Value {
if m == nil {
return cty.NilVal
return cty.MapValEmpty(cty.NilType)
}

ctyMap := make(map[string]cty.Value)
Expand Down Expand Up @@ -224,7 +224,7 @@ func isTuple(v cty.Value) bool {
return v.Type().FriendlyNameForConstraint() == "tuple"
}

func setSeverityLevel(diags hcl.Diagnostics) hcl.Diagnostics {
func SetSeverityLevel(diags hcl.Diagnostics) hcl.Diagnostics {
for _, d := range diags {
if d.Summary == "Missing map element" {
d.Severity = hcl.DiagWarning
Expand Down

0 comments on commit 3e33faf

Please sign in to comment.