Skip to content

Instantly share code, notes, and snippets.

@grantmacken
Last active June 5, 2018 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grantmacken/0763ca9c2a9ae1221375b7f6231d9f28 to your computer and use it in GitHub Desktop.
Save grantmacken/0763ca9c2a9ae1221375b7f6231d9f28 to your computer and use it in GitHub Desktop.
WIP: for local dev container - running prove in a docker container, use nginx::test to test restXQ routes
use Test::Nginx::Socket 'no_plan';
log_level('debug'); # to ensure any log-level can be outputted
#no_diff();
no_long_string();
run_tests();
__DATA__
=== TEST 1: zie.nz
--- config
location = /t1 {
resolver 127.0.0.11 ipv6=off;
content_by_lua_block {
local resolver = require("resty.dns.resolver")
--- docker DNS resolver: 127.0.0.11
local r, err, ans, ok
r, err = resolver:new{nameservers = {'127.0.0.11'}}
if not r then
ngx.log(ngx.ERR, ' - failed to instantiate resolver:' .. err)
ngx.exit()
end
ngx.log(ngx.INFO, ' - instantiated DNS resolver:')
-- local answers , err = r:tcp_query("or", { qtype = r.TYPE_A })
local answers , err = r:tcp_query("or", { qtype = r.TYPE_A })
if not answers then
ngx.log(ngx.ERR, ' - failed to query:' .. err)
end
ngx.log(ngx.INFO, ' - query answered by DNS server')
if answers.errcode then
ngx.log(ngx.ERR,
"server returned error code: " ..
answers.errcode ..
": " ..
answers.errstr)
end
for i, ans in ipairs(answers) do
ngx.log(ngx.INFO , 'NAME: ' .. ans.name )
ngx.log(ngx.INFO , 'ADDRESS: ' .. ans.address )
end
local address = answers[1].address
-- HTTP
local http = require("resty.http")
local httpc = http.new()
local scheme, host, port, path = unpack(httpc:parse_uri('https://zie.nz'))
ngx.log(ngx.INFO , 'PORT: ' .. port )
local ok, err = httpc:connect(address,port)
if not ok then
ngx.log(ngx.ERR, ' - FAILED to connect' .. err)
end
ngx.log(ngx.INFO, ' - connected to ' .. address .. ' on port ' .. port)
ngx.log(ngx.INFO , ' ' .. port )
-- 4 sslhandshake opts
local reusedSession = nil -- defaults to nil
local serverName = host -- for SNI name resolution
local sslVerify = false -- boolean if true make sure the directives set
-- for lua_ssl_trusted_certificate and lua_ssl_verify_depth
local sendStatusReq = '' -- boolean OCSP status request
-- SSL HANDSHAKE
local shake, err = httpc:ssl_handshake( reusedSession, serverName, sslVerify)
if not shake then
ngx.log(ngx.ERR, ' - FAILED to complete SSL handshake' .. err)
end
ngx.log(ngx.INFO, " - SSL Handshake Completed: " .. type(shake) )
httpc:set_timeout(6000)
local response, err = httpc:request({
version = 1.1,
method = "GET",
path = "/",
headers = {
["Host"] = "zie.nz",
["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
},
ssl_verify = sslVerify
})
if not response then
ngx.log(ngx.ERR, ' - FAILED to get response')
end
ngx.log(ngx.INFO, " - Response Status: " .. response.status)
ngx.log(ngx.INFO, " - Response Reason: " .. response.reason)
ngx.status = response.status
--response headers
-- can not yet set response headers so logout and check the logs
for k, v in pairs(response.headers) do
ngx.log(ngx.INFO , k .. ': ' .. v )
end
-- ok, err = ngx.send_headers()
-- ngx.log(ngx.INFO , ngx.headers_sent )
--response body
if response.has_body then
body, err = response:read_body()
if not body then
ngx.log(ngx.ERR, ' - FAILED to get read body: ' .. err)
else
ngx.say(body)
end
end
}}
--- request
GET /t1
--- error_code: 200
--- no_error_log
[error]
[warn]
--- error_log
Host: zie.nz
Strict-Transport-Security: max-age=15768000
Link: <https://zie.nz/webmention> rel='webmention'
Content-Type: text/html;charset=utf-8
Vary: Accept-Encoding
Connection: keep-alive
--- response_body_like
^<!DOCTYPE html>\s+<html>.+</html>$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment