-
-
Save rphillips/366754150ed884bde1f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
Copyright 2014 The Luvit Authors. All Rights Reserved. | |
Licensed 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. | |
--]] | |
local decoder = require('http-codec').decoder | |
local encoder = require('http-codec').encoder | |
local uv = require('uv') | |
local tls = require('tls') | |
require('tap')(function (test) | |
test("Real HTTP request", function (expect) | |
uv.getaddrinfo("www.spark.io", "https", { | |
socktype = "stream", | |
family = "inet", | |
}, expect(function (err, res) | |
assert(not err, err) | |
local options = { | |
host = res[1].addr, | |
port = res[1].port, | |
rejectUnauthorized = false, | |
} | |
p(options) | |
tls.connect(options, function(client) | |
p('here') | |
assert(not err, err) | |
local encode, decode = encoder(), decoder() | |
local req = { | |
method = "GET", path = "/", | |
{"Host", "www.spark.io"}, | |
{"User-Agent", "luvit"}, | |
{"Accept", "*/*"}, | |
} | |
p(req) | |
client:write(encode(req)) | |
local parts = {} | |
local data = "" | |
local finish | |
client:on('data', function(chunk) | |
p(chunk) | |
if not chunk then | |
return finish() | |
end | |
data = data .. chunk | |
repeat | |
local event, extra = decode(data) | |
if event then | |
parts[#parts + 1] = event | |
if event == "" then return finish() end | |
data = extra | |
end | |
until not event | |
end) | |
finish = function () | |
client:destroy() | |
local res = table.remove(parts, 1) | |
p(res) | |
-- luvit.io should redirect to https version | |
local contentLength | |
for i = 1, #res do | |
if string.lower(res[i][1]) == "content-length" then | |
contentLength = tonumber(res[i][2]) | |
break | |
end | |
end | |
for i = 1, #parts do | |
local item = parts[i] | |
contentLength = contentLength - #item | |
p(item) | |
end | |
assert(contentLength == 0) | |
end | |
end) | |
end)) | |
end) | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment