Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run tests on ephemeral ports #2081

Merged
merged 2 commits into from
Feb 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions tests/rest/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -450,18 +450,18 @@ class Example7 : Example7API {
}


void runTests()
void runTests(string url)
{
// Example 1
{
auto api = new RestInterfaceClient!Example1API("http://127.0.0.1:8080");
auto api = new RestInterfaceClient!Example1API(url);
assert(api.getSomeInfo() == "Some Info!");
assert(api.getter == "Getter");
assert(api.postSum(2, 3) == 5);
}
// Example 2
{
auto api = new RestInterfaceClient!Example2API("http://127.0.0.1:8080", MethodStyle.upperUnderscored);
auto api = new RestInterfaceClient!Example2API(url, MethodStyle.upperUnderscored);
Example2API.Aggregate[] data = [
{ "one", 1, Example2API.Aggregate.Type.Type1 },
{ "two", 2, Example2API.Aggregate.Type.Type2 }
Expand All @@ -473,21 +473,21 @@ void runTests()
}
// Example 3
{
auto api = new RestInterfaceClient!Example3API("http://127.0.0.1:8080");
auto api = new RestInterfaceClient!Example3API(url);
assert(api.getMyID(9000) == 9000);
assert(api.nestedModule.getNumber() == 42);
assert(api.nestedModule.getNumber(1) == 1);
}
// Example 4
{
auto api = new RestInterfaceClient!Example4API("http://127.0.0.1:8080");
auto api = new RestInterfaceClient!Example4API(url);
api.myNameDoesNotMatter();
assert(api.getParametersInURL("20", "30") == 50);
assert(api.querySpecialParameterNames(10, true) == -10);
}
// Example 5
{
auto api = new RestInterfaceClient!Example5API("http://127.0.0.1:8080");
auto api = new RestInterfaceClient!Example5API(url);
auto secret = api.getSecret(42, User.init);
assert(secret == "{secret #42 for admin}");
}
Expand All @@ -496,9 +496,9 @@ void runTests()
import vibe.http.client : requestHTTP;
import vibe.stream.operations : readAllUTF8;

auto api = new RestInterfaceClient!Example6API("http://127.0.0.1:8080");
auto api = new RestInterfaceClient!Example6API(url);
// First we make sure parameters are transmitted via headers.
auto res = requestHTTP("http://127.0.0.1:8080/example6_api/portal",
auto res = requestHTTP(url~ "/example6_api/portal",
(scope r) {
r.method = HTTPMethod.GET;
r.headers["Authorization"] = "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==";
Expand Down Expand Up @@ -528,19 +528,19 @@ void runTests()
import vibe.stream.operations : readAllUTF8;

// First we make sure parameters are transmitted via query.
auto res = requestHTTP("http://127.0.0.1:8080/example6_api/answer?qparam=Life_universe_and_the_rest",
auto res = requestHTTP(url~ "/example6_api/answer?qparam=Life_universe_and_the_rest",
(scope r) { r.method = HTTPMethod.POST; });
assert(res.statusCode == 200);
assert(res.bodyReader.readAllUTF8() == `"True"`);
// Then we check that both can communicate together.
auto api = new RestInterfaceClient!Example6API("http://127.0.0.1:8080");
auto api = new RestInterfaceClient!Example6API(url);
auto answer = api.postAnswer("IDK");
assert(answer == "False");
}

// Example 7 -- Custom JSON response
{
auto api = new RestInterfaceClient!Example7API("http://127.0.0.1:8080");
auto api = new RestInterfaceClient!Example7API(url);
auto result = api.get();
assert(result["foo"] == 42 && result["bar"] == 13);
}
Expand All @@ -552,10 +552,10 @@ void runTests()

enum expected = "42fortySomething51.42"; // to!string(51.42) doesn't work at CT

auto api = new RestInterfaceClient!Example6API("http://127.0.0.1:8080");
auto api = new RestInterfaceClient!Example6API(url);
{
// First we make sure parameters are transmitted via query.
auto res = requestHTTP("http://127.0.0.1:8080/example6_api/concat",
auto res = requestHTTP(url ~ "/example6_api/concat",
(scope r) {
import vibe.data.json;
r.method = HTTPMethod.POST;
Expand All @@ -574,7 +574,7 @@ void runTests()
// suppling the whole body
{
// First we make sure parameters are transmitted via query.
auto res = requestHTTP("http://127.0.0.1:8080/example6_api/concat_body",
auto res = requestHTTP(url ~ "/example6_api/concat_body",
(scope r) {
import vibe.data.json;
r.method = HTTPMethod.POST;
Expand Down Expand Up @@ -606,14 +606,13 @@ shared static this()
registerRestInterface(routes, new Example7());

auto settings = new HTTPServerSettings();
settings.port = 8080;
settings.bindAddresses = ["::1", "127.0.0.1"];

listenHTTP(settings, routes);
settings.port = 0;
settings.bindAddresses = ["127.0.0.1"];
immutable serverAddr = listenHTTP(settings, routes).bindAddresses[0];

runTask({
try {
runTests();
runTests("http://" ~ serverAddr.toString);
logInfo("Success.");
} catch (Exception e) {
import core.stdc.stdlib : exit;
Expand Down
35 changes: 19 additions & 16 deletions tests/restclient/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,13 @@ void runTest()

auto settings = new HTTPServerSettings;
settings.disableDistHost = true;
settings.port = 8000;
listenHTTP(settings, router);
settings.port = 0;
settings.bindAddresses = ["127.0.0.1"];
immutable serverAddr = listenHTTP(settings, router).bindAddresses[0];

auto api = new RestInterfaceClient!ITestAPI("http://127.0.0.1:8000/root/");
immutable url = "http://" ~ serverAddr.toString;

auto api = new RestInterfaceClient!ITestAPI(url ~ "/root/");
assert(api.getInfo() == "description");
assert(api.info() == "description2");
assert(api.customParameters("one", "two") == "onetwo");
Expand All @@ -192,19 +195,19 @@ void runTest()
assert(api.sub.get(5) == 5);
assert(api.testKeyword(3, 4) == 7);

assertCorsFails("http://127.0.0.1:8000/cors1/foo", HTTPMethod.GET, "www.non-existent.com");
assertCorsPasses("http://127.0.0.1:8000/cors1/foo", HTTPMethod.GET, "www.foobar.com");
assertCorsPasses("http://127.0.0.1:8000/cors1/foo", HTTPMethod.GET, "www.example.com");
assertCorsPasses("http://127.0.0.1:8000/cors2/foo", HTTPMethod.GET, "www.abcdefg.com");
assertCorsPasses("http://127.0.0.1:8000/cors3/foo", HTTPMethod.GET, "www.foobar.com");
assertCorsFails("http://127.0.0.1:8000/cors3/foo", HTTPMethod.GET, "www.bazbaz.com");

testAllowHeader("http://127.0.0.1:8000/cors1/foo", [HTTPMethod.GET,HTTPMethod.PUT,HTTPMethod.POST]);
testAllowHeader("http://127.0.0.1:8000/cors1/bar/6", [HTTPMethod.POST,HTTPMethod.DELETE,HTTPMethod.PATCH]);
testAllowHeader("http://127.0.0.1:8000/cors1/7/foo", [HTTPMethod.PUT,HTTPMethod.POST,HTTPMethod.DELETE]);
testCors("http://127.0.0.1:8000/cors1/foo", [HTTPMethod.GET,HTTPMethod.PUT,HTTPMethod.POST]);
testCors("http://127.0.0.1:8000/cors1/bar/6", [HTTPMethod.POST,HTTPMethod.DELETE,HTTPMethod.PATCH]);
testCors("http://127.0.0.1:8000/cors1/7/foo", [HTTPMethod.PUT,HTTPMethod.POST,HTTPMethod.DELETE]);
assertCorsFails(url ~ "/cors1/foo", HTTPMethod.GET, "www.non-existent.com");
assertCorsPasses(url ~ "/cors1/foo", HTTPMethod.GET, "www.foobar.com");
assertCorsPasses(url ~ "/cors1/foo", HTTPMethod.GET, "www.example.com");
assertCorsPasses(url ~ "/cors2/foo", HTTPMethod.GET, "www.abcdefg.com");
assertCorsPasses(url ~ "/cors3/foo", HTTPMethod.GET, "www.foobar.com");
assertCorsFails(url ~ "/cors3/foo", HTTPMethod.GET, "www.bazbaz.com");

testAllowHeader(url ~ "/cors1/foo", [HTTPMethod.GET,HTTPMethod.PUT,HTTPMethod.POST]);
testAllowHeader(url ~ "/cors1/bar/6", [HTTPMethod.POST,HTTPMethod.DELETE,HTTPMethod.PATCH]);
testAllowHeader(url ~ "/cors1/7/foo", [HTTPMethod.PUT,HTTPMethod.POST,HTTPMethod.DELETE]);
testCors(url ~ "/cors1/foo", [HTTPMethod.GET,HTTPMethod.PUT,HTTPMethod.POST]);
testCors(url ~ "/cors1/bar/6", [HTTPMethod.POST,HTTPMethod.DELETE,HTTPMethod.PATCH]);
testCors(url ~ "/cors1/7/foo", [HTTPMethod.PUT,HTTPMethod.POST,HTTPMethod.DELETE]);

exitEventLoop(true);
}
Expand Down
7 changes: 4 additions & 3 deletions tests/restcollections/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ void runTest()
router.registerRestInterface(new LocalAPI);

auto settings = new HTTPServerSettings;
settings.port = 0;
settings.bindAddresses = ["127.0.0.1"];
settings.disableDistHost = true;
settings.port = 8000;
listenHTTP(settings, router);
immutable serverAddr = listenHTTP(settings, router).bindAddresses[0];

auto api = new RestInterfaceClient!API("http://127.0.0.1:8000/");
auto api = new RestInterfaceClient!API("http://" ~ serverAddr.toString);
assert(api.items["foo"].subItems.length == 2);
assert(api.items["foo"].subItems[0].name == "hello");
assert(api.items["foo"].subItems[1].name == "world");
Expand Down
12 changes: 6 additions & 6 deletions tests/tcp/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import vibe.stream.operations;
import core.time;
import std.datetime : StopWatch;

enum port = 12675;

enum Test {
receive,
receiveExisting,
Expand All @@ -20,7 +18,7 @@ void test1()
Test test;
Task lt;

listenTCP(port, (conn) {
auto l = listenTCP(0, (conn) {
lt = Task.getThis();
try {
while (!conn.empty) {
Expand Down Expand Up @@ -66,8 +64,9 @@ void test1()
assert(false, e.msg);
}
}, "127.0.0.1");
scope (exit) l.stopListening;

auto conn = connectTCP("127.0.0.1", port);
auto conn = connectTCP(l.bindAddress);

test = Test.receive;
conn.write("next\r\n");
Expand Down Expand Up @@ -105,7 +104,7 @@ void test2()
{
Task lt;
logInfo("Perform test \"disconnect with pending data\"");
listenTCP(port+1, (conn) {
auto l = listenTCP(0, (conn) {
try {
lt = Task.getThis();
sleep(1.seconds);
Expand All @@ -124,8 +123,9 @@ void test2()
assert(false, e.msg);
}
}, "127.0.0.1");
scope (exit) l.stopListening;

auto conn = connectTCP("127.0.0.1", port+1);
auto conn = connectTCP(l.bindAddress);
conn.write("test");
conn.close();

Expand Down
23 changes: 14 additions & 9 deletions tests/tcpproxy/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ void testProtocol(TCPConnection server, bool terminate)

void runTest()
{
import std.algorithm : find;
import std.socket : AddressFamily;

// server for a simple line based protocol
listenTCP(11001, (client) {
auto l1 = listenTCP(0, (client) {
while (!client.empty) {
auto ln = client.readLine();
if (ln == "quit") {
Expand All @@ -50,11 +53,12 @@ void runTest()

client.write(format("Hash: %08X\r\n", typeid(string).getHash(&ln)));
}
});
}).find!(l => l.bindAddress.family == AddressFamily.INET).front;
scope (exit) l1.stopListening;

// proxy server
listenTCP(11002, (client) {
auto server = connectTCP("127.0.0.1", 11001);
auto l2 = listenTCP(0, (client) {
auto server = connectTCP(l1.bindAddress);

// pipe server to client as long as the server connection is alive
auto t = runTask!(TCPConnection, TCPConnection)((client, server) {
Expand All @@ -70,19 +74,20 @@ void runTest()
}
server.write(client);
logInfo("Proxy out");
});
}).find!(l => l.bindAddress.family == AddressFamily.INET).front;
scope (exit) l2.stopListening;

// test server
logInfo("Test protocol implementation on server");
testProtocol(connectTCP("127.0.0.1", 11001), false);
testProtocol(connectTCP(l2.bindAddress), false);
logInfo("Test protocol implementation on server with forced disconnect");
testProtocol(connectTCP("127.0.0.1", 11001), true);
testProtocol(connectTCP(l2.bindAddress), true);

// test proxy
logInfo("Test protocol implementation on proxy");
testProtocol(connectTCP("127.0.0.1", 11002), false);
testProtocol(connectTCP(l2.bindAddress), false);
logInfo("Test protocol implementation on proxy with forced disconnect");
testProtocol(connectTCP("127.0.0.1", 11002), true);
testProtocol(connectTCP(l2.bindAddress), true);
}

int main()
Expand Down
4 changes: 2 additions & 2 deletions tests/vibe.core.net.1726/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ubyte[] buf;

void performTest(bool reverse)
{
auto l = listenTCP(11375, (conn) {
auto l = listenTCP(0, (conn) {
bool read_ex = false;
bool write_ex = false;
auto rt = runTask!TCPConnection((conn) {
Expand Down Expand Up @@ -42,7 +42,7 @@ void performTest(bool reverse)

runTask({
try {
auto conn = connectTCP("127.0.0.1", 11375);
auto conn = connectTCP(l.bindAddress);
sleep(reverse ? 20.msecs : 100.msecs);
conn.close();
} catch (Exception e) assert(false, e.msg);
Expand Down
19 changes: 9 additions & 10 deletions tests/vibe.http.client.1389/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,32 @@ shared static this()
{
// determine external network interface
auto ec = connectTCP("vibed.org", 80);
auto li = ec.localAddress;
auto externalAddr = ec.localAddress;
ec.close();
li.port = 0;
logInfo("External interface: %s", li.toString());
logInfo("External interface: %s", externalAddr.toString());

auto settings = new HTTPServerSettings;
// 10k + issue number -> Avoid bind errors
settings.port = 11389;
settings.bindAddresses = [li.toAddressString()];
listenHTTP(settings, (req, res) {
settings.port = 0;
settings.bindAddresses = [externalAddr.toAddressString()];
immutable serverAddr = listenHTTP(settings, (req, res) {
if (req.clientAddress.toAddressString() == "127.0.0.1")
res.writeBody("local");
else res.writeBody("remote");
});
}).bindAddresses[0];

runTask({
scope(exit) exitEventLoop(true);

auto url = "http://"~li.toAddressString~":11389/";
auto url = "http://"~serverAddr.toString;
logInfo(url);

auto cs = new HTTPClientSettings;
cs.networkInterface = resolveHost("127.0.0.1");
auto res = requestHTTP(url, null, cs).bodyReader.readAllUTF8();
assert(res == "local", "Unexpected reply: "~res);

auto cs2 = new HTTPClientSettings;
cs2.networkInterface = li;
cs2.networkInterface = resolveHost(externalAddr.toAddressString());
res = requestHTTP(url, null, cs2).bodyReader.readAllUTF8();
assert(res == "remote", "Unexpected reply: "~res);
});
Expand Down
6 changes: 3 additions & 3 deletions tests/vibe.http.client.1426/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import vibe.stream.operations;

shared static this()
{
listenTCP(11426, (TCPConnection c) {
immutable serverAddr = listenTCP(0, (TCPConnection c) {
c.write("HTTP/1.1 200 OK\r\nConnection: Close\r\n\r\nqwerty");
}, "127.0.0.1");
}, "127.0.0.1").bindAddress;

runTask({
requestHTTP("http://127.0.0.1:11426",
requestHTTP("http://" ~ serverAddr.toString,
(scope req) {},
(scope res) {
assert(res.bodyReader.readAllUTF8() == "qwerty");
Expand Down
8 changes: 4 additions & 4 deletions tests/vibe.http.server.1388/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ void main()
{
version (none) {
auto s1 = new HTTPServerSettings;
s1.port = 0;
s1.bindAddresses = ["::1"];
s1.port = 11388;
listenHTTP(s1, &handler);
immutable serverAddr = listenHTTP(s1, &handler).bindAddresses[0];

runTask({
auto conn = connectTCP("::1", 11388);
auto conn = connectTCP(serverAddr);
conn.write("GET / HTTP/1.1\r\nHost: [::1]\r\n\r\n");
string res = cast(string)conn.readLine();
assert(res == "HTTP/1.1 200 OK", res);
Expand All @@ -35,7 +35,7 @@ void main()
}

import vibe.core.log : logWarn;
logWarn("Test disabled due to issues listening on ::1 on Travis-CI");
logWarn("Test disabled due to missing IPv6 support (loopback) on Travis-CI");
}

void handler(scope HTTPServerRequest req, scope HTTPServerResponse res)
Expand Down
Loading