From 4f75627e5cf1d0d9a0470ba2d0144ae1b0e53651 Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Thu, 19 Oct 2017 14:39:07 +0300 Subject: [PATCH] Replace `httpbin.org` to local pegasus server --- .travis.yml | 17 ++++++-- .travis/platform.sh | 15 +++++++ .travis/setup_uv.sh | 23 ++++++++++ appveyor.yml | 15 +++++-- msvc/lcurl.vcproj | 14 ++++-- test/server.lua | 94 ++++++++++++++++++++++++++++++++++++++++- test/test_curl.lua | 13 +++--- test/test_easy.lua | 14 +++--- test/test_mime.lua | 2 +- test/test_pause02.c.lua | 3 +- 10 files changed, 187 insertions(+), 23 deletions(-) create mode 100644 .travis/platform.sh create mode 100644 .travis/setup_uv.sh diff --git a/.travis.yml b/.travis.yml index 7e0facd..01c85f6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -39,15 +39,26 @@ before_install: - pip install --user hererocks - hererocks here -r^ --$LUA - source here/bin/activate + - luarocks show lluv > /dev/null 2>&1 || bash .travis/setup_uv.sh install: - luarocks make rockspecs/lua-curl-scm-0.rockspec CFLAGS="$LCURL_CC_FLAGS" LIBFLAG="$LCURL_LD_FLAGS" before_script: - luarocks show luacov-coveralls > /dev/null 2>&1 || luarocks install luacov-coveralls - - luarocks show lunitx > /dev/null 2>&1 || luarocks install lunitx - - luarocks show luafilesystem > /dev/null 2>&1 || luarocks install luafilesystem - - luarocks show dkjson > /dev/null 2>&1 || luarocks install dkjson --deps-mode=none + - luarocks show lunitx > /dev/null 2>&1 || luarocks install lunitx + - luarocks show luafilesystem > /dev/null 2>&1 || luarocks install luafilesystem + - luarocks show dkjson > /dev/null 2>&1 || luarocks install dkjson --deps-mode=none + - luarocks show pegasus > /dev/null 2>&1 || luarocks install pegasus http.parser + --server=http://luarocks.org/manifests/moteus + - luarocks show pegasus-router > /dev/null 2>&1 || luarocks install pegasus-router + --server=http://luarocks.org/dev + - luarocks show lluv > /dev/null 2>&1 || luarocks install lluv UV_DIR=$TRAVIS_BUILD_DIR/libuv + --server=http://luarocks.org/dev + - luarocks show lluv-pegasus > /dev/null 2>&1 || luarocks install lluv-pegasus + --server=http://luarocks.org/dev --deps-mode=none + - lua test/server.lua & + - curl -s http://127.0.0.1:7090/get script: - cd test diff --git a/.travis/platform.sh b/.travis/platform.sh new file mode 100644 index 0000000..7259a7d --- /dev/null +++ b/.travis/platform.sh @@ -0,0 +1,15 @@ +if [ -z "${PLATFORM:-}" ]; then + PLATFORM=$TRAVIS_OS_NAME; +fi + +if [ "$PLATFORM" == "osx" ]; then + PLATFORM="macosx"; +fi + +if [ -z "$PLATFORM" ]; then + if [ "$(uname)" == "Linux" ]; then + PLATFORM="linux"; + else + PLATFORM="macosx"; + fi; +fi diff --git a/.travis/setup_uv.sh b/.travis/setup_uv.sh new file mode 100644 index 0000000..69aa72e --- /dev/null +++ b/.travis/setup_uv.sh @@ -0,0 +1,23 @@ +#! /bin/bash + +source .travis/platform.sh + +cd $TRAVIS_BUILD_DIR + +git clone https://github.com/libuv/libuv.git -b v1.x + +cd libuv + +mkdir -p lib +mkdir -p build +git clone https://chromium.googlesource.com/external/gyp build/gyp + +if [ "$PLATFORM" == "macosx" ]; then + ./gyp_uv.py -f xcode && xcodebuild -ARCHS="x86_64" -project uv.xcodeproj -configuration Release -target All + cp ./build/Release/libuv.a ./lib; +else + ./gyp_uv.py -f make && BUILDTYPE=Release CFLAGS=-fPIC make -C out + cp ./out/Release/libuv.a ./lib; +fi + +cd $TRAVIS_BUILD_DIR diff --git a/appveyor.yml b/appveyor.yml index e4e867b..6004fb2 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -58,10 +58,12 @@ before_test: - luarocks show dkjson >nul 2>&1 || luarocks install dkjson - luarocks show luafilesystem >nul 2>&1 || luarocks install luafilesystem - luarocks show lua-path >nul 2>&1 || luarocks install lua-path - - luarocks show pegasus >nul 2>&1 || luarocks install pegasus http.parser \ + - luarocks show pegasus >nul 2>&1 || luarocks install pegasus http.parser --server=http://luarocks.org/manifests/moteus - - ps: $TestServer = Start-Process lua -ArgumentList test/server.lua -PassThru - - curl -s http://127.0.0.1:7090 + - luarocks show pegasus-router >nul 2>&1 || luarocks install pegasus-router + --server=http://luarocks.org/dev + - ps: $TestServer = Start-Process lua -ArgumentList test/server.lua -RedirectStandardOutput "$env:APPVEYOR_BUILD_FOLDER\server.stdout.txt" -RedirectStandardError "$env:APPVEYOR_BUILD_FOLDER\server.stderr.txt" -PassThru + - curl -s http://127.0.0.1:7090/get test_script: - echo "Testing..." @@ -75,5 +77,10 @@ after_test: - cd %APPVEYOR_BUILD_FOLDER% - .appveyor\pack_artifact.bat lua-curl bin-rock +on_failure: + - ps: $path = "$env:APPVEYOR_BUILD_FOLDER\server.stderr.txt"; if (Test-Path $path -PathType Leaf) { Push-AppveyorArtifact $path; } else { echo "File $path does not exist"; } + - ps: $path = "$env:APPVEYOR_BUILD_FOLDER\server.stdout.txt"; if (Test-Path $path -PathType Leaf) { Push-AppveyorArtifact $path; } else { echo "File $path does not exist"; } + on_finish: - - ps: Stop-Process -Id $TestServer.Id \ No newline at end of file + - curl -s http://127.0.0.1:7090/get + - ps: Stop-Process -Id $TestServer.Id diff --git a/msvc/lcurl.vcproj b/msvc/lcurl.vcproj index 6d58985..7c22f2e 100644 --- a/msvc/lcurl.vcproj +++ b/msvc/lcurl.vcproj @@ -1,7 +1,7 @@ + + @@ -251,6 +255,10 @@ RelativePath="..\src\lcinfoeasy.h" > + + diff --git a/test/server.lua b/test/server.lua index e29bd6c..7e71ad6 100644 --- a/test/server.lua +++ b/test/server.lua @@ -6,10 +6,102 @@ end local uv = prequire "lluv" local Pegasus = require (uv and "lluv.pegasus" or "pegasus") +local Router = require "pegasus.plugins.router" +local json = require "dkjson" +-- local pp = require "pp" -local server = Pegasus:new{host = '127.0.0.1', port = 7090} +local function decode_form(form) + return string.match(form, '\r\nContent%-Disposition:%s*form%-data;%s*name="(.-)".-\r\n\r\n(.-)\r\n') +end + +local function decode_params(str) + local params = {} + for k, v in string.gmatch(str, '([^=]+)=([^&]+)&?') do + params[k] = v + end + return params +end + +local function rand_bytes(n) + local res = {} + for i = 1, n do + res[#res + 1] = string.char(math.random(254)) + end + return table.concat(res) +end + +local r = Router:new() + +local server = Pegasus:new{ + plugins = { r }; + host = '127.0.0.1', port = 7090, timout = 10 +} + +r:get('/get', function(request, response) + local headers = request:headers() + local params = request:params() + local path = request:path() + local ip = request.ip + + local result = json.encode({ + args = params; + headers = headers; + origin = ip; + url = 'http://127.0.0.1' .. path; + }, {indent = true}) + + response:statusCode(200) + response:contentType('application/json') + response:write(result) +end) + +r:get('/bytes/:size', function(request, response, params) + local headers = request:headers() + local size = tonumber(params.size) or 1024 + local result = rand_bytes(size) + + response:statusCode(200) + response:contentType('application/octet-stream') + response:write(result) +end) + +r:post('/post', function(request, response, params) + local headers = request:headers() + local params = request:params() + local path = request:path() + local ip = request.ip + + local body = {} + while true do + local result, status = request:receiveBody() + if result then body[#body + 1] = result + elseif status ~= 'timeout' then break end + end + body = table.concat(body) + + local name, data, form = decode_form(body) + if name then + form = {[name] = data} + else + form = decode_params(body) + end + + local result = json.encode({ + args = params; + headers = headers; + origin = ip; + form = form; + url = 'http://127.0.0.1' .. path; + }, {indent = true}) + + response:statusCode(200) + response:contentType('application/json') + response:write(result) +end) server:start(function(request, response) + local headers = request:headers() + response:statusCode(200) response:addHeader('Content-Type', 'text/plain') response:write('Hello from Pegasus') diff --git a/test/test_curl.lua b/test/test_curl.lua index 3e65497..8047e15 100644 --- a/test/test_curl.lua +++ b/test/test_curl.lua @@ -24,6 +24,8 @@ local utils = require "utils" -- not sure is it bug or not local text_plain = utils.is_curl_eq(7,56,0) and 'test/plain' or 'text/plain' +local GET_URL = "http://127.0.0.1:7090/get" + local ENABLE = true local _ENV = TEST_CASE'version' if ENABLE then @@ -35,7 +37,7 @@ end end -local _ENV = TEST_CASE'easy' if ENABLE then +local _ENV = TEST_CASE'easy' if ENABLE then local e1, e2 function teardown() @@ -124,7 +126,7 @@ end local _ENV = TEST_CASE'multi_iterator' if ENABLE then -local url = "http://httpbin.org/get" +local url = GET_URL local c, t, m @@ -144,8 +146,7 @@ function teardown() end function test_add_handle() - - local base_url = 'http://httpbin.org/get?key=' + local base_url = url .. '?key=' local urls = { base_url .. "1", base_url .. "2", @@ -196,7 +197,9 @@ function test_add_handle() end function test_info_read() - local url = 'http://httpbin.org/get?key=1' + + local url = GET_URL .. '?key=1' + c = assert(curl.easy{url=url, writefunction=function() end}) assert_equal(m, m:add_handle(c)) diff --git a/test/test_easy.lua b/test/test_easy.lua index d61d335..68b54cb 100644 --- a/test/test_easy.lua +++ b/test/test_easy.lua @@ -30,6 +30,10 @@ local fname = "./test.download" local weak_ptr, gc_collect, is_curl_ge, read_file, stream, Stream = utils.import('weak_ptr', 'gc_collect', 'is_curl_ge', 'read_file', 'stream', 'Stream') +-- local POST_URL = "http://httpbin.org/post" + +local POST_URL = "http://127.0.0.1:7090/post" + local ENABLE = true local _ENV = TEST_CASE'curl error' if ENABLE then @@ -416,7 +420,7 @@ local _ENV = TEST_CASE'read_stream_callback' if ENABLE and is_curl_ge(7,30,0) th -- tested on WinXP(x32)/Win8(x64) libcurl/7.37.1 / libcurl/7.30.0 -local url = "http://httpbin.org/post" +local url = POST_URL local m, c, f, t @@ -865,10 +869,10 @@ end function test() do local fields = {} - for i = 1, 100 do fields[#fields + 1] = "key" .. i .. "=value"..i end + for i = 1, 100 do fields[#fields + 1] = string.format('key%d=value%d', i, i) end fields = table.concat(fields, '&') c = assert(curl.easy{ - url = "http://httpbin.org/post", + url = POST_URL, postfields = fields, writefunction = function()end, }) @@ -884,10 +888,10 @@ function test_unset() local pfields do local fields = {} - for i = 1, 100 do fields[#fields + 1] = "key" .. i .. "=value"..i end + for i = 1, 100 do fields[#fields + 1] = string.format('key%d=value%d', i, i) end fields = table.concat(fields, '&') c = assert(curl.easy{ - url = "http://httpbin.org/post", + url = POST_URL, postfields = fields, writefunction = function()end, }) diff --git a/test/test_mime.lua b/test/test_mime.lua index 80964d8..8dc458b 100644 --- a/test/test_mime.lua +++ b/test/test_mime.lua @@ -17,7 +17,7 @@ local utils = require "utils" local weak_ptr, gc_collect, dump_mime_ = utils.import('weak_ptr', 'gc_collect', 'dump_mime') -local dump_mime_url = 'http://127.0.0.1:7090' +local dump_mime_url = 'http://127.0.0.1:7090/post' local function is_freed(c) return not not string.find(tostring(c), '%(freed%)') diff --git a/test/test_pause02.c.lua b/test/test_pause02.c.lua index 383c652..3a1fb4f 100644 --- a/test/test_pause02.c.lua +++ b/test/test_pause02.c.lua @@ -6,7 +6,8 @@ local curl = require "lcurl" local WAIT_COUNT = 15 local SIZE = 10 * 1024 -local RESOURCE_URL = "http://httpbin.org/bytes/" .. SIZE +-- local RESOURCE_URL = "http://httpbin.org/bytes/" .. SIZE +local RESOURCE_URL = "http://127.0.0.1:7090/bytes/" .. SIZE local State = { PAUSE = 0, -- write function should return CURL_WRITEFUNC_PAUSE