bisakhmondal commented on a change in pull request #5940: URL: https://github.com/apache/apisix/pull/5940#discussion_r802626941
########## File path: t/plugin/mocking.t ########## @@ -0,0 +1,402 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +BEGIN { + if ($ENV{TEST_NGINX_CHECK_LEAK}) { + $SkipReason = "unavailable for the hup tests"; + + } else { + $ENV{TEST_NGINX_USE_HUP} = 1; + undef $ENV{TEST_NGINX_USE_STAP}; + } +} + +use t::APISIX 'no_plan'; + +repeat_each(1); +no_long_string(); +no_shuffle(); +no_root_location(); +log_level('info'); +run_tests; + +__DATA__ + +=== TEST 1: set route(return response example:"hello world") +--- 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": { + "mocking": { + "delay": 1, + "content_type": "text/plain", + "response_status": 200, + "response_example": "hello world" + } + }, + "uri": "/hello" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 200 +--- response_body +passed +--- no_error_log Review comment: Hi @Drery, It seems I somehow missed your ping, really sorry for that. It's really simple. Do give it a try, I can guide you. A bit of context: here we are using Perl scripting (a [fork](https://github.com/iresty/test-nginx) of test-nginx framework). As you can see the location path for each test is `/t` - we don't need to define `--- request` block for every unit tests. Instead we can manually inject it at the very beginning. What the block actually does (see my inline comments)? ```perl add_block_preprocessor(sub { my ($block) = @_; # this snippet sets the `request` field if not set from an unit test. also it gives you the flexibility to override in certain tests (say POST, PUT etc) if (!$block->request) { $block->set_value("request", "GET /t"); } # here also, you don't have to manually put `--- no_error_log` block for each unit tests. It will inject it automatically. if (!$block->error_log && !$block->no_error_log) { $block->set_value("no_error_log", "[error]\n[alert]"); } }); ``` If you are really interested to understand how the whole thing works you can run a test file and side by side compare `t/servroot/conf/nginx.conf` with https://github.com/apache/apisix/blob/master/t/APISIX.pm An extra example: If you notice, the test config is written inside `--- config` block. But you may wonder, how we are bridging the gap between writing the tests (in Perl) and running those tests in a test environment (requires an nginx.conf with some Lua blocks). This is where the Perl scripting helps - before running any tests we generate a new nginx.conf from the `--- config` set inside the test case, please take a look https://github.com/apache/apisix/blob/defafb1e426c4ffe4aa740c9bb9ad26992fa2c2e/t/APISIX.pm#L641-L650 Hope this helps. Thank you. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
