Add unit test for wxWebRequest
The environment variable WX_TEST_WEBREQUEST_URL has to be set to https://httpbin.org or a local instance of it to be run.
This commit is contained in:
@@ -52,6 +52,7 @@ set(TEST_SRC
|
|||||||
misc/typeinfotest.cpp
|
misc/typeinfotest.cpp
|
||||||
net/ipc.cpp
|
net/ipc.cpp
|
||||||
net/socket.cpp
|
net/socket.cpp
|
||||||
|
net/webrequest.cpp
|
||||||
regex/regextest.cpp
|
regex/regextest.cpp
|
||||||
regex/wxregextest.cpp
|
regex/wxregextest.cpp
|
||||||
scopeguard/scopeguardtest.cpp
|
scopeguard/scopeguardtest.cpp
|
||||||
|
@@ -99,6 +99,7 @@ TEST_OBJECTS = \
|
|||||||
test_typeinfotest.o \
|
test_typeinfotest.o \
|
||||||
test_ipc.o \
|
test_ipc.o \
|
||||||
test_socket.o \
|
test_socket.o \
|
||||||
|
test_webrequest.o \
|
||||||
test_regextest.o \
|
test_regextest.o \
|
||||||
test_wxregextest.o \
|
test_wxregextest.o \
|
||||||
test_scopeguardtest.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)
|
test_socket.o: $(srcdir)/net/socket.cpp $(TEST_ODEP)
|
||||||
$(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/net/socket.cpp
|
$(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)
|
test_regextest.o: $(srcdir)/regex/regextest.cpp $(TEST_ODEP)
|
||||||
$(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/regex/regextest.cpp
|
$(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/regex/regextest.cpp
|
||||||
|
|
||||||
|
@@ -82,6 +82,7 @@ TEST_OBJECTS = \
|
|||||||
$(OBJS)\test_typeinfotest.obj \
|
$(OBJS)\test_typeinfotest.obj \
|
||||||
$(OBJS)\test_ipc.obj \
|
$(OBJS)\test_ipc.obj \
|
||||||
$(OBJS)\test_socket.obj \
|
$(OBJS)\test_socket.obj \
|
||||||
|
$(OBJS)\test_webrequest.obj \
|
||||||
$(OBJS)\test_regextest.obj \
|
$(OBJS)\test_regextest.obj \
|
||||||
$(OBJS)\test_wxregextest.obj \
|
$(OBJS)\test_wxregextest.obj \
|
||||||
$(OBJS)\test_scopeguardtest.obj \
|
$(OBJS)\test_scopeguardtest.obj \
|
||||||
@@ -673,6 +674,9 @@ $(OBJS)\test_ipc.obj: .\net\ipc.cpp
|
|||||||
$(OBJS)\test_socket.obj: .\net\socket.cpp
|
$(OBJS)\test_socket.obj: .\net\socket.cpp
|
||||||
$(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\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
|
$(OBJS)\test_regextest.obj: .\regex\regextest.cpp
|
||||||
$(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\regex\regextest.cpp
|
$(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\regex\regextest.cpp
|
||||||
|
|
||||||
|
@@ -74,6 +74,7 @@ TEST_OBJECTS = \
|
|||||||
$(OBJS)\test_typeinfotest.o \
|
$(OBJS)\test_typeinfotest.o \
|
||||||
$(OBJS)\test_ipc.o \
|
$(OBJS)\test_ipc.o \
|
||||||
$(OBJS)\test_socket.o \
|
$(OBJS)\test_socket.o \
|
||||||
|
$(OBJS)\test_webrequest.o \
|
||||||
$(OBJS)\test_regextest.o \
|
$(OBJS)\test_regextest.o \
|
||||||
$(OBJS)\test_wxregextest.o \
|
$(OBJS)\test_wxregextest.o \
|
||||||
$(OBJS)\test_scopeguardtest.o \
|
$(OBJS)\test_scopeguardtest.o \
|
||||||
@@ -650,6 +651,9 @@ $(OBJS)\test_ipc.o: ./net/ipc.cpp
|
|||||||
$(OBJS)\test_socket.o: ./net/socket.cpp
|
$(OBJS)\test_socket.o: ./net/socket.cpp
|
||||||
$(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<
|
$(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
|
$(OBJS)\test_regextest.o: ./regex/regextest.cpp
|
||||||
$(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<
|
$(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<
|
||||||
|
|
||||||
|
@@ -77,6 +77,7 @@ TEST_OBJECTS = \
|
|||||||
$(OBJS)\test_typeinfotest.obj \
|
$(OBJS)\test_typeinfotest.obj \
|
||||||
$(OBJS)\test_ipc.obj \
|
$(OBJS)\test_ipc.obj \
|
||||||
$(OBJS)\test_socket.obj \
|
$(OBJS)\test_socket.obj \
|
||||||
|
$(OBJS)\test_webrequest.obj \
|
||||||
$(OBJS)\test_regextest.obj \
|
$(OBJS)\test_regextest.obj \
|
||||||
$(OBJS)\test_wxregextest.obj \
|
$(OBJS)\test_wxregextest.obj \
|
||||||
$(OBJS)\test_scopeguardtest.obj \
|
$(OBJS)\test_scopeguardtest.obj \
|
||||||
@@ -864,6 +865,9 @@ $(OBJS)\test_ipc.obj: .\net\ipc.cpp
|
|||||||
$(OBJS)\test_socket.obj: .\net\socket.cpp
|
$(OBJS)\test_socket.obj: .\net\socket.cpp
|
||||||
$(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\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
|
$(OBJS)\test_regextest.obj: .\regex\regextest.cpp
|
||||||
$(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\regex\regextest.cpp
|
$(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\regex\regextest.cpp
|
||||||
|
|
||||||
|
129
tests/net/webrequest.cpp
Normal file
129
tests/net/webrequest.cpp
Normal file
@@ -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<wxWebRequest> 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<wxInputStream>(new wxFileInputStream("horse.png")),
|
||||||
|
"image/png");
|
||||||
|
request->SetMethod("PUT");
|
||||||
|
Run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // wxUSE_WEBREQUEST
|
@@ -70,6 +70,7 @@
|
|||||||
misc/typeinfotest.cpp
|
misc/typeinfotest.cpp
|
||||||
net/ipc.cpp
|
net/ipc.cpp
|
||||||
net/socket.cpp
|
net/socket.cpp
|
||||||
|
net/webrequest.cpp
|
||||||
regex/regextest.cpp
|
regex/regextest.cpp
|
||||||
regex/wxregextest.cpp
|
regex/wxregextest.cpp
|
||||||
scopeguard/scopeguardtest.cpp
|
scopeguard/scopeguardtest.cpp
|
||||||
|
@@ -562,6 +562,9 @@
|
|||||||
<File
|
<File
|
||||||
RelativePath=".\weakref\weakref.cpp">
|
RelativePath=".\weakref\weakref.cpp">
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\net\webrequest.cpp">
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\regex\wxregextest.cpp">
|
RelativePath=".\regex\wxregextest.cpp">
|
||||||
</File>
|
</File>
|
||||||
|
@@ -1218,6 +1218,10 @@
|
|||||||
RelativePath=".\weakref\weakref.cpp"
|
RelativePath=".\weakref\weakref.cpp"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\net\webrequest.cpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\regex\wxregextest.cpp"
|
RelativePath=".\regex\wxregextest.cpp"
|
||||||
>
|
>
|
||||||
|
@@ -1190,6 +1190,10 @@
|
|||||||
RelativePath=".\weakref\weakref.cpp"
|
RelativePath=".\weakref\weakref.cpp"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\net\webrequest.cpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\regex\wxregextest.cpp"
|
RelativePath=".\regex\wxregextest.cpp"
|
||||||
>
|
>
|
||||||
|
Reference in New Issue
Block a user