-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JQ tests, and make compilation conditional
- Loading branch information
Showing
8 changed files
with
160 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// +build !jq | ||
|
||
package main | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/TykTechnologies/tyk/apidef" | ||
) | ||
|
||
type TransformJQMiddleware struct { | ||
BaseMiddleware | ||
} | ||
|
||
func (t *TransformJQMiddleware) Name() string { | ||
return "TransformJQMiddleware" | ||
} | ||
|
||
func (t *TransformJQMiddleware) EnabledForSpec() bool { | ||
for _, version := range t.Spec.VersionData.Versions { | ||
if len(version.ExtendedPaths.TransformJQ) > 0 { | ||
log.Warning("JQ transform not supported.") | ||
return false | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail | ||
func (t *TransformJQMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Request, _ interface{}) (error, int) { | ||
return nil, 200 | ||
} | ||
|
||
type TransformJQSpec struct { | ||
apidef.TransformJQMeta | ||
} | ||
|
||
func (a *APIDefinitionLoader) compileTransformJQPathSpec(paths []apidef.TransformJQMeta, stat URLStatus) []URLSpec { | ||
return []URLSpec{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// +build jq | ||
|
||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/TykTechnologies/tyk/apidef" | ||
"github.com/TykTechnologies/tyk/test" | ||
) | ||
|
||
func TestJQMiddleware(t *testing.T) { | ||
ts := newTykTestServer() | ||
defer ts.Close() | ||
|
||
buildAndLoadAPI(func(spec *APISpec) { | ||
spec.Proxy.ListenPath = "/" | ||
spec.EnableContextVars = true | ||
updateAPIVersion(spec, "v1", func(v *apidef.VersionInfo) { | ||
v.UseExtendedPaths = true | ||
v.ExtendedPaths.TransformJQ = []apidef.TransformJQMeta{{ | ||
Path: "/jq", | ||
Method: "POST", | ||
Filter: `{"body": (.body + {"TRANSFORMED-REQUEST-BY-JQ": true, path: ._tyk_context.path, user_agent: ._tyk_context.headers_User_Agent}), "rewrite_headers": {"X-added-rewrite-headers": .body.foo}, "tyk_context": { "foo-val": .body.foo}}`, | ||
}} | ||
}) | ||
}) | ||
|
||
bodyContextVar := `\"path\":\"/jq\"` | ||
headersBodyVar := `"X-Added-Rewrite-Headers":"bar"` | ||
|
||
ts.Run(t, []test.TestCase{ | ||
{Path: "/jq", Method: "POST", Data: `{"foo": "bar"}`, Code: 200, BodyMatch: bodyContextVar}, | ||
{Path: "/jq", Method: "POST", Data: `{"foo": "bar"}`, Code: 200, BodyMatch: headersBodyVar}, | ||
{Path: "/jq", Method: "POST", Data: `wrong json`, Code: 415}, | ||
}...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//+build !jq | ||
|
||
package main | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/TykTechnologies/tyk/user" | ||
) | ||
|
||
type ResponseTransformJQMiddleware struct { | ||
Spec *APISpec | ||
} | ||
|
||
func (h *ResponseTransformJQMiddleware) Init(c interface{}, spec *APISpec) error { | ||
h.Spec = spec | ||
|
||
return nil | ||
} | ||
|
||
func (h *ResponseTransformJQMiddleware) HandleResponse(rw http.ResponseWriter, res *http.Response, req *http.Request, ses *user.SessionState) error { | ||
log.Warning("JQ transforms not supported") | ||
return nil | ||
} |