aboutsummaryrefslogtreecommitdiff
path: root/tests/curl
diff options
context:
space:
mode:
Diffstat (limited to 'tests/curl')
-rw-r--r--tests/curl/buildfile7
-rw-r--r--tests/curl/driver.cxx121
-rw-r--r--tests/curl/testscript57
3 files changed, 185 insertions, 0 deletions
diff --git a/tests/curl/buildfile b/tests/curl/buildfile
new file mode 100644
index 0000000..6b19bea
--- /dev/null
+++ b/tests/curl/buildfile
@@ -0,0 +1,7 @@
+# file : tests/curl/buildfile
+# copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
+# license : MIT; see accompanying LICENSE file
+
+exe{driver}: cxx{driver} ../../butl/lib{butl} test{testscript}
+
+include ../../butl/
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;
+}
diff --git a/tests/curl/testscript b/tests/curl/testscript
new file mode 100644
index 0000000..e41aeb6
--- /dev/null
+++ b/tests/curl/testscript
@@ -0,0 +1,57 @@
+# file : tests/curl/testscript
+# copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
+# license : MIT; see accompanying LICENSE file
+
+#\
+
+TFTP server (tftp-hpa) setup: from the test out_base, run (sudo is required
+for --secure/chroot):
+
+sudo /usr/sbin/in.tftpd \
+ --foreground \
+ --address 127.0.0.1:55123 \
+ --user "$(whoami)" \
+ --permissive \
+ --create \
+ --secure \
+ "$(pwd)"
+
+#\
+
+: tftp
+:
+{
+ echo 'foo' >=foo-src;
+
+ $* 'tftp' &foo &bar 2>>EOE;
+
+ curl -s -S tftp://localhost:55123/test-driver/tftp/foo
+ curl: (68) TFTP: File Not Found
+
+ curl -s -S --upload-file foo-src tftp://localhost:55123/test-driver/tftp/foo
+
+ curl -s -S --upload-file - tftp://localhost:55123/test-driver/tftp/bar
+
+ curl -s -S tftp://localhost:55123/test-driver/tftp/foo
+
+ curl -s -S tftp://localhost:55123/test-driver/tftp/foo
+ EOE
+
+ diff -u foo-src foo;
+ diff -u - bar <'bar'
+}
+
+: http
+:
+{
+ $* 'http' 2>>EOE
+
+ curl -s -S --fail --location https://build2.org/bogus
+ curl: (22) The requested URL returned error: 404 Not Found
+
+ curl -s -S --fail --location https://build2.org
+
+ curl -s -S --fail --location --data-binary @- https://build2.org/bogus
+ curl: (22) The requested URL returned error: 404 Not Found
+ EOE
+}