diff --git a/t/lib/server.lua b/t/lib/server.lua index 119d64f8bcfa..ff45464464f0 100644 --- a/t/lib/server.lua +++ b/t/lib/server.lua @@ -1327,4 +1327,15 @@ function _M.plugin_proxy_rewrite_multi_header() end +-- mock of Lago's batch events endpoint (POST /api/v1/events/batch); logs the +-- Authorization header and request body so lago.t can assert on what the plugin +-- actually sent, then answers 200 with an empty batch like Lago does +function _M.api_v1_events_batch() + ngx.req.read_body() + ngx.log(ngx.WARN, "lago auth: ", ngx.req.get_headers()["Authorization"] or "") + ngx.log(ngx.WARN, "lago body: ", ngx.req.get_body_data() or "") + ngx.say('{"events": []}') +end + + return _M diff --git a/t/plugin/lago.t b/t/plugin/lago.t index d97875e05ea6..046e43e68dca 100644 --- a/t/plugin/lago.t +++ b/t/plugin/lago.t @@ -14,10 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # -use t::APISIX; - -# This test cannot be executed normally at the moment, so it will be temporarily skipped and fixed in a later PR. -plan(skip_all => 'skip test case'); +use t::APISIX 'no_plan'; repeat_each(1); no_long_string(); @@ -69,12 +66,58 @@ property "event_properties" validation failed: wrong type: expected object, got -=== TEST 2: test ---- timeout: 302 ---- max_size: 2048000 ---- exec -cd t && pnpm test plugin/lago.spec.mts 2>&1 ---- no_error_log -failed to execute the script with status ---- response_body eval -qr/PASS plugin\/lago.spec.mts/ +=== TEST 2: set up a route whose lago plugin sends events to the mock endpoint +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "lago": { + "endpoint_addrs": ["http://127.0.0.1:1980"], + "token": "test-token", + "event_transaction_id": "txn-${request_id}", + "event_subscription_id": "sub-1", + "event_code": "test", + "event_properties": { + "tier": "normal", + "status": "${status}" + }, + "batch_max_size": 1 + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/hello" + }]] + ) + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- response_body +passed + + + +=== TEST 3: hitting the route batches one event and sends it to the mock +--- request +GET /hello +--- wait: 2 +--- response_body +hello world +--- error_log +lago auth: Bearer test-token +"code":"test" +"external_subscription_id":"sub-1" +"transaction_id":"txn- +"tier":"normal" +"status":"200"