diff --git a/build/cmake/tests/base/CMakeLists.txt b/build/cmake/tests/base/CMakeLists.txt index 9cab7a5079..747a8202ef 100644 --- a/build/cmake/tests/base/CMakeLists.txt +++ b/build/cmake/tests/base/CMakeLists.txt @@ -52,6 +52,7 @@ set(TEST_SRC misc/typeinfotest.cpp net/ipc.cpp net/socket.cpp + net/webrequest.cpp regex/regextest.cpp regex/wxregextest.cpp scopeguard/scopeguardtest.cpp diff --git a/tests/Makefile.in b/tests/Makefile.in index 698e3b0832..e17bfa9086 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -99,6 +99,7 @@ TEST_OBJECTS = \ test_typeinfotest.o \ test_ipc.o \ test_socket.o \ + test_webrequest.o \ test_regextest.o \ test_wxregextest.o \ test_scopeguardtest.o \ @@ -625,6 +626,9 @@ test_ipc.o: $(srcdir)/net/ipc.cpp $(TEST_ODEP) test_socket.o: $(srcdir)/net/socket.cpp $(TEST_ODEP) $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/net/socket.cpp +test_webrequest.o: $(srcdir)/net/webrequest.cpp $(TEST_ODEP) + $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/net/webrequest.cpp + test_regextest.o: $(srcdir)/regex/regextest.cpp $(TEST_ODEP) $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/regex/regextest.cpp diff --git a/tests/makefile.bcc b/tests/makefile.bcc index a0892cbd57..ef3b570077 100644 --- a/tests/makefile.bcc +++ b/tests/makefile.bcc @@ -82,6 +82,7 @@ TEST_OBJECTS = \ $(OBJS)\test_typeinfotest.obj \ $(OBJS)\test_ipc.obj \ $(OBJS)\test_socket.obj \ + $(OBJS)\test_webrequest.obj \ $(OBJS)\test_regextest.obj \ $(OBJS)\test_wxregextest.obj \ $(OBJS)\test_scopeguardtest.obj \ @@ -673,6 +674,9 @@ $(OBJS)\test_ipc.obj: .\net\ipc.cpp $(OBJS)\test_socket.obj: .\net\socket.cpp $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\net\socket.cpp +$(OBJS)\test_webrequest.obj: .\net\webrequest.cpp + $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\net\webrequest.cpp + $(OBJS)\test_regextest.obj: .\regex\regextest.cpp $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\regex\regextest.cpp diff --git a/tests/makefile.gcc b/tests/makefile.gcc index a4e16537b8..c674f1a1c0 100644 --- a/tests/makefile.gcc +++ b/tests/makefile.gcc @@ -74,6 +74,7 @@ TEST_OBJECTS = \ $(OBJS)\test_typeinfotest.o \ $(OBJS)\test_ipc.o \ $(OBJS)\test_socket.o \ + $(OBJS)\test_webrequest.o \ $(OBJS)\test_regextest.o \ $(OBJS)\test_wxregextest.o \ $(OBJS)\test_scopeguardtest.o \ @@ -650,6 +651,9 @@ $(OBJS)\test_ipc.o: ./net/ipc.cpp $(OBJS)\test_socket.o: ./net/socket.cpp $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< +$(OBJS)\test_webrequest.o: ./net/webrequest.cpp + $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< + $(OBJS)\test_regextest.o: ./regex/regextest.cpp $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< diff --git a/tests/makefile.vc b/tests/makefile.vc index e197998717..4fcebf124f 100644 --- a/tests/makefile.vc +++ b/tests/makefile.vc @@ -77,6 +77,7 @@ TEST_OBJECTS = \ $(OBJS)\test_typeinfotest.obj \ $(OBJS)\test_ipc.obj \ $(OBJS)\test_socket.obj \ + $(OBJS)\test_webrequest.obj \ $(OBJS)\test_regextest.obj \ $(OBJS)\test_wxregextest.obj \ $(OBJS)\test_scopeguardtest.obj \ @@ -864,6 +865,9 @@ $(OBJS)\test_ipc.obj: .\net\ipc.cpp $(OBJS)\test_socket.obj: .\net\socket.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\net\socket.cpp +$(OBJS)\test_webrequest.obj: .\net\webrequest.cpp + $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\net\webrequest.cpp + $(OBJS)\test_regextest.obj: .\regex\regextest.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\regex\regextest.cpp diff --git a/tests/net/webrequest.cpp b/tests/net/webrequest.cpp new file mode 100644 index 0000000000..147c3f9e5c --- /dev/null +++ b/tests/net/webrequest.cpp @@ -0,0 +1,129 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: tests/net/webrequest.cpp +// Purpose: wxWebRequest test +// Author: Tobias Taschner +// Created: 2018-10-24 +// Copyright: (c) 2018 wxWidgets development team +// Licence: wxWindows licence +///////////////////////////////////////////////////////////////////////////// + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +#include "testprec.h" + +#ifdef __BORLANDC__ +#pragma hdrstop +#endif + +#ifndef WX_PRECOMP + #include "wx/wx.h" +#endif // WX_PRECOMP + +#include "wx/webrequest.h" +#include "wx/wfstream.h" + +// This test requires the URL to an httpbin instance. +// httpbin is a HTTP Request & Response Service available at: +// https://httpbin.org +// It can also be run locally via a simple docker run +// +// For this test to run the the base URL has to be specified with +// an environment variable, e.g.: +// WX_TEST_WEBREQUEST_URL=https://httpbin.org + +#if wxUSE_WEBREQUEST + +class RequestFixture +{ +public: + RequestFixture() { } + + void Create(const wxString& subURL) + { + wxString baseURL; + wxGetEnv("WX_TEST_WEBREQUEST_URL", &baseURL); + CreateAbs(baseURL + subURL); + } + + void CreateAbs(const wxString& url) + { + request.reset(wxWebSession::GetDefault().CreateRequest(url)); + request->Bind(wxEVT_WEBREQUEST_STATE, &RequestFixture::OnRequestState, this); + } + + void OnRequestState(wxWebRequestEvent& evt) + { + switch (evt.GetState()) + { + case wxWebRequest::State_Completed: + case wxWebRequest::State_Failed: + case wxWebRequest::State_Cancelled: + loop.Exit(); + break; + } + } + + void Run(wxWebRequest::State requiredState = wxWebRequest::State_Completed, + int requiredStatus = 200) + { + REQUIRE( request->GetState() == wxWebRequest::State_Idle ); + request->Start(); + loop.Run(); + REQUIRE( request->GetState() == requiredState ); + if (requiredStatus) + REQUIRE( request->GetResponse()->GetStatus() == requiredStatus ); + } + + wxEventLoop loop; + wxObjectDataPtr request; +}; + +TEST_CASE_METHOD(RequestFixture, "WebRequest", "[net][.]") +{ + wxString baseURL; + if ( !wxGetEnv("WX_TEST_WEBREQUEST_URL", &baseURL) ) + { + WARN("Skipping WebRequest test because required WX_TEST_WEBREQUEST_URL" + " environment variable is not defined."); + return; + } + + SECTION("GET 64kb to memory") + { + Create("/bytes/65536"); + Run(); + REQUIRE( request->GetResponse()->GetContentLength() == 65536 ); + } + + SECTION("GET 404 error") + { + Create("/status/404"); + Run(wxWebRequest::State_Failed, 404); + } + + SECTION("Connect to invalid host") + { + CreateAbs("http://127.0.0.1:51234"); + Run(wxWebRequest::State_Failed, 0); + } + + SECTION("POST form data") + { + Create("/post"); + request->SetData("app=WebRequestSample&version=1", "application/x-www-form-urlencoded"); + Run(); + } + + SECTION("PUT file data") + { + Create("/put"); + request->SetData(wxSharedPtr(new wxFileInputStream("horse.png")), + "image/png"); + request->SetMethod("PUT"); + Run(); + } +} + +#endif // wxUSE_WEBREQUEST diff --git a/tests/test.bkl b/tests/test.bkl index b8c42324c8..24bcabc3c1 100644 --- a/tests/test.bkl +++ b/tests/test.bkl @@ -70,6 +70,7 @@ misc/typeinfotest.cpp net/ipc.cpp net/socket.cpp + net/webrequest.cpp regex/regextest.cpp regex/wxregextest.cpp scopeguard/scopeguardtest.cpp diff --git a/tests/test_vc7_test.vcproj b/tests/test_vc7_test.vcproj index 9443e3b8c9..98b24d250f 100644 --- a/tests/test_vc7_test.vcproj +++ b/tests/test_vc7_test.vcproj @@ -562,6 +562,9 @@ + + diff --git a/tests/test_vc8_test.vcproj b/tests/test_vc8_test.vcproj index d085fdc4a3..c7edd58a5a 100644 --- a/tests/test_vc8_test.vcproj +++ b/tests/test_vc8_test.vcproj @@ -1218,6 +1218,10 @@ RelativePath=".\weakref\weakref.cpp" > + + diff --git a/tests/test_vc9_test.vcproj b/tests/test_vc9_test.vcproj index 4570595312..a8947a97ed 100644 --- a/tests/test_vc9_test.vcproj +++ b/tests/test_vc9_test.vcproj @@ -1190,6 +1190,10 @@ RelativePath=".\weakref\weakref.cpp" > + +