aboutsummaryrefslogtreecommitdiff
path: root/tests/curl/driver.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2017-04-18 10:40:18 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2017-04-18 10:40:18 +0200
commited93e07b1b7a9e0ba99609a9223e43247ff4224e (patch)
treeaa203bdab5a5fc4f5fd8af16baf6903a7ee3dde0 /tests/curl/driver.cxx
parent4408607c51a7c6e293adae41403b21d4a2c9a429 (diff)
Implement curl process
Diffstat (limited to 'tests/curl/driver.cxx')
-rw-r--r--tests/curl/driver.cxx121
1 files changed, 121 insertions, 0 deletions
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 <iostream>
+#include <system_error>
+
+#include <butl/path>
+#include <butl/utility> // operator<<(ostream, exception)
+#include <butl/curl>
+
+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;
+}