Created
September 28, 2018 15:51
-
-
Save rezan/f605a39bce5c10e68bb1a0f00c0b906e to your computer and use it in GitHub Desktop.
Forward JSON req and resp body to another service (VCP 4.1)
This file contains hidden or 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
| varnishtest "Test JSON forwarding (4.1)" | |
| server s1 { | |
| rxreq | |
| txresp -body { | |
| { | |
| "key":"respvalue", | |
| "key2":23, | |
| "2": { | |
| "nested":"goal" | |
| } | |
| } | |
| } | |
| } -start | |
| server s2 { | |
| rxreq | |
| txresp -status 200 -reason "POST OK" | |
| expect req.url == "/some/service" | |
| expect req.method == "POST" | |
| expect req.http.Content-Type == "application/vnd.kafka.json.v2+json" | |
| expect req.body ~ "reqvalue" | |
| expect req.body ~ "respvalue" | |
| expect req.body ~ "h1234" | |
| } -start | |
| varnish v1 -vcl { | |
| import edgestash; | |
| import http; | |
| import json; | |
| import std; | |
| backend default { | |
| .host = "${s1_addr}"; | |
| .port = "${s1_port}"; | |
| } | |
| sub vcl_recv { | |
| std.cache_req_body(10KB); | |
| json.parse_req_body(); | |
| set req.http.X-req-json = json.get(".", ""); | |
| } | |
| sub vcl_backend_response { | |
| edgestash.index_json(); | |
| } | |
| sub vcl_deliver { | |
| if (edgestash.is_json()) { | |
| json.parse_resp_index(); | |
| set req.http.NL = {" | |
| "}; | |
| http.init(0); | |
| http.req_set_url(0, "http://${s2_addr}:${s2_port}/some/service"); | |
| http.req_set_header(0, "Content-Type", "application/vnd.kafka.json.v2+json"); | |
| http.req_set_sparam(0, "POSTFIELDS", | |
| "date: " + now + req.http.NL + | |
| "reqbody: " + req.http.X-req-json + req.http.NL + | |
| "respbody: " + json.get(".", "") + req.http.NL + | |
| "X-header: " + req.http.X-header); | |
| http.req_send(0); | |
| http.resp_wait(0); | |
| set resp.http.X-resp-status = http.resp_get_status(0); | |
| set resp.http.X-resp-reason = http.resp_get_reason(0); | |
| } | |
| } | |
| } -start | |
| client c1 { | |
| txreq -hdr "X-header: h1234" -body { | |
| { | |
| "some":"reqvalue" | |
| } | |
| } | |
| rxresp | |
| expect resp.status == 200 | |
| expect resp.http.X-resp-status == "200" | |
| expect resp.http.X-resp-reason == "POST OK" | |
| } -run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment