1 /**
2  * Tests all Network implementations.
3  *
4  * License:
5  *   This Source Code Form is subject to the terms of
6  *   the Mozilla Public License, v. 2.0. If a copy of
7  *   the MPL was not distributed with this file, You
8  *   can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * Authors:
11  *   Vladimir Panteleev <vladimir@thecybershadow.net>
12  */
13 
14 module ae.sys.net.test;
15 
16 import std.file;
17 
18 static import ae.sys.net.ae;
19 static import ae.sys.net.curl;
20 version(Windows)
21 static import ae.sys.net.wininet;
22 
23 debug static import std.stdio;
24 
25 // Server-side test scripts are here:
26 // https://gist.github.com/58df32ed1dbe64fffd0545f87d9321ad
27 
28 void test(string moduleName, string className)()
29 {
30 	debug std.stdio.stderr.writeln("Testing " ~ className);
31 
32 	mixin("import ae.sys.net." ~ moduleName ~ ";");
33 	mixin("alias Net = " ~ className ~ ";");
34 	auto net = new Net();
35 
36 	debug std.stdio.stderr.writeln(" - getFile");
37 	{
38 		assert(net.getFile("http://thecybershadow.net/d/nettest/testUrl1") == "Hello world\n");
39 	}
40 
41 	debug std.stdio.stderr.writeln(" - downloadFile");
42 	{
43 		enum fn = "test.txt";
44 		if (fn.exists) fn.remove();
45 		scope(exit) if (fn.exists) fn.remove();
46 
47 		net.downloadFile("http://thecybershadow.net/d/nettest/testUrl1", fn);
48 		assert(fn.readText() == "Hello world\n");
49 	}
50 
51 	debug std.stdio.stderr.writeln(" - urlOK");
52 	{
53 		assert( net.urlOK("http://thecybershadow.net/d/nettest/testUrl1"));
54 		assert(!net.urlOK("http://thecybershadow.net/d/nettest/testUrlNX"));
55 		static if (moduleName == "wininet")
56 			assert( net.urlOK("https://thecybershadow.net/d/nettest/testUrl1"));
57 	}
58 
59 	debug std.stdio.stderr.writeln(" - resolveRedirect");
60 	{
61 		auto result = net.resolveRedirect("http://thecybershadow.net/d/nettest/testUrl3");
62 		assert(result == "http://thecybershadow.net/d/nettest/testUrl2", result);
63 	}
64 }
65 
66 unittest
67 {
68 	test!("ae", "AENetwork");
69 	test!("curl", "CurlNetwork");
70 	version(Windows)
71 	test!("wininet", "WinINetNetwork");
72 }