From ed93e07b1b7a9e0ba99609a9223e43247ff4224e Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 18 Apr 2017 10:40:18 +0200 Subject: Implement curl process --- tests/curl/driver.cxx | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 tests/curl/driver.cxx (limited to 'tests/curl/driver.cxx') diff --git a/tests/curl/driver.cxx b/tests/curl/driver.cxx new file mode 100644 index 0000000..3711e71 --- /dev/null +++ b/tests/curl/driver.cxx @@ -0,0 +1,121 @@ +// file : tests/curl/driver.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include +#include + +#include +#include // operator<<(ostream, exception) +#include + +using namespace std; +using namespace butl; + +// Usage: argv[0] tftp|http +// + +static void +print_cmd (const char* c[], std::size_t n) +{ + cerr << endl; + process::print (cerr, c, n); + cerr << endl; +} + +static void +tftp () +{ + string u ("tftp://localhost:55123/test-driver/tftp"); + + auto p = print_cmd; + + // GET non-existent. + // + { + curl c (p, nullfd, fdnull (), 2, curl::get, u + "/foo"); + assert (!c.wait ()); + } + + // PUT from file. + // + { + curl c (p, path ("foo-src"), nullfd, 2, curl::put, u + "/foo"); + assert (c.wait ()); + } + + // PUT from stream. + // + { + curl c (p, path ("-"), nullfd, 2, curl::put, u + "/bar"); + c.out << "bar" << endl; + c.out.close (); + assert (c.wait ()); + } + + // GET to stream. + // + { + curl c (p, nullfd, path ("-"), 2, curl::get, u + "/foo"); + string s; + getline (c.in, s); + c.in.close (); + assert (c.wait () && s == "foo"); + } + + // GET to /dev/null. + // + { + curl c (p, nullfd, fdnull (), 2, curl::get, u + "/foo"); + assert (c.wait ()); + } +} + +static void +http () +{ + string u ("https://build2.org"); + + auto p = print_cmd; + + // GET non-existent. + // + { + curl c (p, nullfd, fdnull (), 2, curl::get, u + "/bogus"); + assert (!c.wait ()); + } + + // GET to /dev/null. + // + { + curl c (p, nullfd, fdnull (), 2, curl::get, u); + assert (c.wait ()); + } + + // POST from stream. + // + { + curl c (p, path ("-"), 1, 2, curl::post, u + "/bogus"); + c.out << "bogus" << endl; + c.out.close (); + assert (!c.wait ()); + } +} + +int +main (int argc, const char* argv[]) +try +{ + assert (argc == 2); + + string a (argv[1]); + + if (a == "tftp") tftp (); + else if (a == "http") http (); + else assert (false); +} +catch (const system_error& e) +{ + cerr << argv[0] << ':' << argv[1] << ": " << e << endl; + return 1; +} -- cgit v1.1