mocka icon indicating copy to clipboard operation
mocka copied to clipboard

Using mocka for Openresty testing

Open syedmhashim opened this issue 5 years ago • 7 comments

Hello! The Readme document states that mocka has nginx embedded methods for individual testing of Openresty. However, I cannot find any support material or examples/references for that. Can you please guide me on how to use it for ngx testing. Thanks!

syedmhashim avatar Feb 18 '20 05:02 syedmhashim

Sorry for the late answer yes, I will update with a bunch of openresty testing docs. Is that ok?

atrifan avatar Oct 28 '20 22:10 atrifan

Yes please update the docs. Thanks!

syedmhashim avatar Oct 29 '20 12:10 syedmhashim

@SyedMHashim a pull request is ongoing after is done I will merge to dev and master. There is no need for a new version of mocka it works with previous versions as well. If you have any other questions please don't hesitate to contact me at [email protected]

atrifan avatar Nov 12 '20 13:11 atrifan

Hi Alexandru! If you can also update the README.md with usage examples with regards to testing ngx, then that would be great! Thanks a load!

syedmhashim avatar Nov 12 '20 15:11 syedmhashim

Can you please be more specific about your testing scenario so I can better pinpoint the docs?

Get Outlook for iOShttps://aka.ms/o0ukef


From: Syed Muhammad Hashim [email protected] Sent: Thursday, November 12, 2020 5:19:29 PM To: adobe/mocka [email protected] Cc: Alexandru Trifan [email protected]; Comment [email protected] Subject: Re: [adobe/mocka] Using mocka for Openresty testing (#115)

Hi Alexandru! If you can also update the README.md with usage examples with regards to testing ngx, then that would be great! Thanks a load!

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/adobe/mocka/issues/115#issuecomment-726143462, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAQP7EH43SV6NTFNBOPA3PDSPP4IDANCNFSM4KW5Q4RQ.

atrifan avatar Nov 12 '20 17:11 atrifan

Hi Alexandru. I'll show you the way I've have done the testing of the lua code that's used within my nginx configuration files. Can you please verify if this is how it should be done. The tests run successfully tho.

security.lua (This would be used within the nginx configuration)

local module = {}

function module.authenticate()
   --[[
       some lua code that authenticates the requests
   ]]--
end

return module

The sample usage of above code in nginx.conf

server {
  listen 9191
  location /some/path {
    require("security").authenticate()
    proxy_pass ...
  }
}

Now for testing this lua code I created ngx/mocked.lua as the default ngx doesn't fulfill my needs:

local METHOD = nil
local URI_ARGS = {}

local ngx = {
    exit = function(arg) error({status=arg}) end,
    header = {},
    HTTP_OK = 200,
    HTTP_UNAUTHORIZED = 401,
    HTTP_INTERNAL_SERVER_ERROR = 500,
    log = function(...) end,
    req = {
        set_header = function(arg1, arg2) ngx.header[arg1] = arg2 end,
        get_headers = function() return ngx.header end,
        set_method = function(m) METHOD=m end,
        get_method = function() return METHOD end,
        set_uri_args = function(args) URI_ARGS = args end,
        get_uri_args = function() return URI_ARGS, nil end
    },
    say = print,
    var = {}
}

return ngx

And in authenticate_test.lua I'm using it like this:

authenticate = require("security").authenticate

beforeEach(function()
    mockNgx(require("ngx.mocked"))
end)

test("Authentication Successful", function()
    ngx.req.set_method("GET")
   -- some testing code
end)

syedmhashim avatar Jan 12 '21 09:01 syedmhashim

the above is correct only if you plan to use ngx just inside the testing code, but if you want to replace the global variable you should not use local ngx you should just say ngx.exit = ... and so on since ngx is a global. You also cannot replace var on the global field because it is actual a c code invocation. It would be better if you would show me the actual testing and tell me more on what you want to verify, you want to verify ngx calls if they are executing or what?

atrifan avatar Jan 13 '21 15:01 atrifan