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 import std.process : environment; 18 19 static import ae.sys.net.ae; 20 static import ae.sys.net.curl; 21 version(Windows) 22 static import ae.sys.net.wininet; 23 static import ae.sys.net.cachedcurl; 24 25 debug static import std.stdio; 26 27 // Server-side test scripts are here: 28 // https://gist.github.com/58df32ed1dbe64fffd0545f87d9321ad 29 30 void test(string moduleName, string className)() 31 { 32 debug std.stdio.stderr.writeln("Testing " ~ className); 33 34 mixin("import ae.sys.net." ~ moduleName ~ ";"); 35 mixin("alias Net = " ~ className ~ ";"); 36 auto net = new Net(); 37 38 debug std.stdio.stderr.writeln(" - getFile"); 39 { 40 assert(net.getFile("http://thecybershadow.net/d/nettest/testUrl1") == "Hello world\n"); 41 } 42 43 debug std.stdio.stderr.writeln(" - downloadFile"); 44 { 45 enum fn = "test.txt"; 46 if (fn.exists) fn.remove(); 47 scope(exit) if (fn.exists) fn.remove(); 48 49 net.downloadFile("http://thecybershadow.net/d/nettest/testUrl1", fn); 50 assert(fn.readText() == "Hello world\n"); 51 } 52 53 debug std.stdio.stderr.writeln(" - urlOK"); 54 { 55 assert( net.urlOK("http://thecybershadow.net/d/nettest/testUrl1")); 56 assert(!net.urlOK("http://thecybershadow.net/d/nettest/testUrlNX")); 57 static if (moduleName == "wininet") 58 assert( net.urlOK("https://thecybershadow.net/d/nettest/testUrl1")); 59 } 60 61 debug std.stdio.stderr.writeln(" - resolveRedirect"); 62 { 63 auto result = net.resolveRedirect("http://thecybershadow.net/d/nettest/testUrl3"); 64 assert(result == "http://thecybershadow.net/d/nettest/testUrl2", result); 65 } 66 67 debug std.stdio.stderr.writeln(" - post"); 68 { 69 auto result = cast(string)net.post("http://thecybershadow.net/d/nettest/testUrl4", "Hello world\n"); 70 assert(result == "Hello world\n", result); 71 } 72 } 73 74 unittest 75 { 76 // Don't do network requests on the project tester. 77 // See https://github.com/CyberShadow/ae/issues/30 78 if (environment.get("JENKINS_URL")) 79 return; 80 81 test!("ae", "AENetwork"); 82 test!("curl", "CurlNetwork"); 83 version(Windows) 84 test!("wininet", "WinINetNetwork"); 85 86 import ae.utils.meta : classInit; 87 auto cacheDir = classInit!(ae.sys.net.cachedcurl.CachedCurlNetwork).cacheDir; 88 if (cacheDir.exists) cacheDir.rmdirRecurse(); 89 scope(exit) if (cacheDir.exists) cacheDir.rmdirRecurse(); 90 test!("cachedcurl", "CachedCurlNetwork"); 91 }