2018-10-24 17:55:27 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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
|
|
|
|
|
2020-12-26 10:00:03 -05:00
|
|
|
#if wxUSE_WEBREQUEST
|
|
|
|
|
2018-10-24 17:55:27 -04:00
|
|
|
#include "wx/webrequest.h"
|
2018-10-30 16:54:40 -04:00
|
|
|
#include "wx/filename.h"
|
2018-10-24 17:55:27 -04:00
|
|
|
#include "wx/wfstream.h"
|
|
|
|
|
2020-12-26 10:43:03 -05:00
|
|
|
// This test uses https://httpbin.org by default, but this can be overridden by
|
|
|
|
// setting WX_TEST_WEBREQUEST_URL, e.g. when running httpbin locally in a
|
|
|
|
// docker container. This variable can also be set to a special value "0" to
|
|
|
|
// disable running the test entirely.
|
|
|
|
static const char* WX_TEST_WEBREQUEST_URL_DEFAULT = "https://httpbin.org";
|
2018-10-24 17:55:27 -04:00
|
|
|
|
2020-12-29 19:10:02 -05:00
|
|
|
class RequestFixture : public wxEvtHandler
|
2018-10-24 17:55:27 -04:00
|
|
|
{
|
|
|
|
public:
|
2018-10-30 16:54:40 -04:00
|
|
|
RequestFixture()
|
|
|
|
{
|
|
|
|
expectedFileSize = 0;
|
|
|
|
dataSize = 0;
|
|
|
|
}
|
2018-10-24 17:55:27 -04:00
|
|
|
|
2020-12-30 08:41:17 -05:00
|
|
|
static bool UsingNSURLSession()
|
|
|
|
{
|
|
|
|
return wxWebSession::GetDefault().GetLibraryVersionInfo().GetName()
|
|
|
|
== "URLSession";
|
|
|
|
}
|
|
|
|
|
2020-12-26 10:43:03 -05:00
|
|
|
bool InitBaseURL()
|
|
|
|
{
|
|
|
|
if ( !wxGetEnv("WX_TEST_WEBREQUEST_URL", &baseURL) )
|
|
|
|
baseURL = WX_TEST_WEBREQUEST_URL_DEFAULT;
|
|
|
|
|
|
|
|
return baseURL != "0";
|
|
|
|
}
|
|
|
|
|
2018-10-24 17:55:27 -04:00
|
|
|
void Create(const wxString& subURL)
|
|
|
|
{
|
|
|
|
CreateAbs(baseURL + subURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreateAbs(const wxString& url)
|
|
|
|
{
|
2020-12-29 19:10:02 -05:00
|
|
|
request = wxWebSession::GetDefault().CreateRequest(this, url);
|
|
|
|
Bind(wxEVT_WEBREQUEST_STATE, &RequestFixture::OnRequestState, this);
|
|
|
|
Bind(wxEVT_WEBREQUEST_DATA, &RequestFixture::OnData, this);
|
2018-10-24 17:55:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnRequestState(wxWebRequestEvent& evt)
|
|
|
|
{
|
|
|
|
switch (evt.GetState())
|
|
|
|
{
|
2018-10-25 17:33:05 -04:00
|
|
|
case wxWebRequest::State_Unauthorized:
|
2018-10-24 17:55:27 -04:00
|
|
|
case wxWebRequest::State_Completed:
|
2020-12-29 19:10:02 -05:00
|
|
|
if ( request.GetStorage() == wxWebRequest::Storage_File )
|
2018-10-30 16:54:40 -04:00
|
|
|
{
|
|
|
|
wxFileName fn(evt.GetResponseFileName());
|
|
|
|
REQUIRE( fn.GetSize() == expectedFileSize );
|
|
|
|
}
|
|
|
|
wxFALLTHROUGH;
|
2018-10-24 17:55:27 -04:00
|
|
|
case wxWebRequest::State_Failed:
|
|
|
|
case wxWebRequest::State_Cancelled:
|
|
|
|
loop.Exit();
|
|
|
|
break;
|
2020-12-12 12:12:44 -05:00
|
|
|
|
|
|
|
case wxWebRequest::State_Idle:
|
|
|
|
case wxWebRequest::State_Active:
|
|
|
|
break;
|
2018-10-24 17:55:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 16:54:40 -04:00
|
|
|
void OnData(wxWebRequestEvent& evt)
|
|
|
|
{
|
|
|
|
// Count all bytes recieved via data event for Storage_None
|
|
|
|
dataSize += evt.GetDataSize();
|
|
|
|
}
|
|
|
|
|
2018-10-24 17:55:27 -04:00
|
|
|
void Run(wxWebRequest::State requiredState = wxWebRequest::State_Completed,
|
|
|
|
int requiredStatus = 200)
|
|
|
|
{
|
2020-12-29 19:10:02 -05:00
|
|
|
REQUIRE( request.GetState() == wxWebRequest::State_Idle );
|
|
|
|
request.Start();
|
2018-10-24 17:55:27 -04:00
|
|
|
loop.Run();
|
2020-12-29 19:10:02 -05:00
|
|
|
REQUIRE( request.GetState() == requiredState );
|
2018-10-24 17:55:27 -04:00
|
|
|
if (requiredStatus)
|
2020-12-29 19:10:02 -05:00
|
|
|
REQUIRE( request.GetResponse().GetStatus() == requiredStatus );
|
2018-10-24 17:55:27 -04:00
|
|
|
}
|
|
|
|
|
2020-12-26 10:43:03 -05:00
|
|
|
wxString baseURL;
|
2018-10-24 17:55:27 -04:00
|
|
|
wxEventLoop loop;
|
2020-12-29 19:10:02 -05:00
|
|
|
wxWebRequest request;
|
2018-10-30 16:54:40 -04:00
|
|
|
wxInt64 expectedFileSize;
|
|
|
|
wxInt64 dataSize;
|
2018-10-24 17:55:27 -04:00
|
|
|
};
|
|
|
|
|
2020-12-26 10:43:03 -05:00
|
|
|
TEST_CASE_METHOD(RequestFixture, "WebRequest", "[net][webrequest]")
|
2018-10-24 17:55:27 -04:00
|
|
|
{
|
2020-12-26 10:43:03 -05:00
|
|
|
// Skip the test entirely if disabled.
|
|
|
|
if ( !InitBaseURL() )
|
2018-10-24 17:55:27 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
SECTION("GET 64kb to memory")
|
|
|
|
{
|
|
|
|
Create("/bytes/65536");
|
|
|
|
Run();
|
2020-12-29 19:10:02 -05:00
|
|
|
REQUIRE( request.GetResponse().GetContentLength() == 65536 );
|
|
|
|
REQUIRE( request.GetBytesExpectedToReceive() == 65536 );
|
|
|
|
REQUIRE( request.GetBytesReceived() == 65536 );
|
2018-10-24 17:55:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2020-12-29 19:10:02 -05:00
|
|
|
request.SetData("app=WebRequestSample&version=1", "application/x-www-form-urlencoded");
|
2018-10-24 17:55:27 -04:00
|
|
|
Run();
|
|
|
|
}
|
|
|
|
|
2018-10-29 18:20:47 -04:00
|
|
|
SECTION("GET data as string")
|
|
|
|
{
|
|
|
|
Create("/base64/VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw==");
|
|
|
|
Run();
|
2020-12-29 19:10:02 -05:00
|
|
|
REQUIRE( request.GetResponse().AsString() == "The quick brown fox jumps over the lazy dog" );
|
2018-10-29 18:20:47 -04:00
|
|
|
}
|
|
|
|
|
2018-10-30 16:54:40 -04:00
|
|
|
SECTION("GET 99KB to file")
|
|
|
|
{
|
|
|
|
expectedFileSize = 99 * 1024;
|
|
|
|
Create(wxString::Format("/bytes/%lld", expectedFileSize));
|
2020-12-29 19:10:02 -05:00
|
|
|
request.SetStorage(wxWebRequest::Storage_File);
|
2018-10-30 16:54:40 -04:00
|
|
|
Run();
|
2020-12-29 19:10:02 -05:00
|
|
|
REQUIRE( request.GetBytesReceived() == expectedFileSize );
|
2018-10-30 16:54:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Process 99KB data")
|
|
|
|
{
|
|
|
|
int processingSize = 99 * 1024;
|
|
|
|
Create(wxString::Format("/bytes/%d", processingSize));
|
2020-12-29 19:10:02 -05:00
|
|
|
request.SetStorage(wxWebRequest::Storage_None);
|
2018-10-30 16:54:40 -04:00
|
|
|
Run();
|
2020-12-29 19:10:02 -05:00
|
|
|
REQUIRE( request.GetBytesReceived() == processingSize );
|
2018-10-30 16:54:40 -04:00
|
|
|
REQUIRE( dataSize == processingSize );
|
|
|
|
}
|
|
|
|
|
2018-10-24 17:55:27 -04:00
|
|
|
SECTION("PUT file data")
|
|
|
|
{
|
|
|
|
Create("/put");
|
2021-01-03 19:57:36 -05:00
|
|
|
wxScopedPtr<wxInputStream> is(new wxFileInputStream("horse.png"));
|
2020-12-12 12:46:28 -05:00
|
|
|
REQUIRE( is->IsOk() );
|
|
|
|
|
2021-01-03 19:57:36 -05:00
|
|
|
request.SetData(is.release(), "image/png");
|
2020-12-29 19:10:02 -05:00
|
|
|
request.SetMethod("PUT");
|
2018-10-24 17:55:27 -04:00
|
|
|
Run();
|
|
|
|
}
|
2018-10-25 17:33:05 -04:00
|
|
|
|
|
|
|
SECTION("Server auth BASIC")
|
|
|
|
{
|
2020-12-30 08:41:17 -05:00
|
|
|
if ( UsingNSURLSession() )
|
|
|
|
{
|
|
|
|
WARN("NSURLSession backend doesn't support authentication, skipping.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-09 18:33:39 -05:00
|
|
|
Create("/basic-auth/wxtest/wxwidgets");
|
2018-10-25 17:33:05 -04:00
|
|
|
Run(wxWebRequest::State_Unauthorized, 401);
|
2020-12-29 19:10:02 -05:00
|
|
|
REQUIRE( request.GetAuthChallenge().IsOk() );
|
2021-01-09 18:41:05 -05:00
|
|
|
|
|
|
|
SECTION("Good password")
|
|
|
|
{
|
|
|
|
request.GetAuthChallenge().SetCredentials("wxtest", "wxwidgets");
|
|
|
|
loop.Run();
|
|
|
|
CHECK( request.GetResponse().GetStatus() == 200 );
|
|
|
|
CHECK( request.GetState() == wxWebRequest::State_Completed );
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Bad password")
|
|
|
|
{
|
|
|
|
request.GetAuthChallenge().SetCredentials("wxtest", "foobar");
|
|
|
|
loop.Run();
|
|
|
|
CHECK( request.GetResponse().GetStatus() == 401 );
|
|
|
|
CHECK( request.GetState() == wxWebRequest::State_Unauthorized );
|
|
|
|
}
|
2018-10-25 17:33:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Server auth DIGEST")
|
|
|
|
{
|
2020-12-30 08:41:17 -05:00
|
|
|
if ( UsingNSURLSession() )
|
|
|
|
{
|
|
|
|
WARN("NSURLSession backend doesn't support authentication, skipping.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-25 17:33:05 -04:00
|
|
|
Create("/digest-auth/auth/wxtest/wxwidgets");
|
|
|
|
Run(wxWebRequest::State_Unauthorized, 401);
|
2020-12-29 19:10:02 -05:00
|
|
|
REQUIRE( request.GetAuthChallenge().IsOk() );
|
2021-01-09 18:41:05 -05:00
|
|
|
|
|
|
|
SECTION("Good password")
|
|
|
|
{
|
|
|
|
request.GetAuthChallenge().SetCredentials("wxtest", "wxwidgets");
|
|
|
|
loop.Run();
|
|
|
|
CHECK( request.GetResponse().GetStatus() == 200 );
|
|
|
|
CHECK( request.GetState() == wxWebRequest::State_Completed );
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Bad password")
|
|
|
|
{
|
|
|
|
request.GetAuthChallenge().SetCredentials("foo", "bar");
|
|
|
|
loop.Run();
|
|
|
|
CHECK( request.GetResponse().GetStatus() == 401 );
|
|
|
|
CHECK( request.GetState() == wxWebRequest::State_Unauthorized );
|
|
|
|
}
|
2018-10-25 17:33:05 -04:00
|
|
|
}
|
2018-10-24 17:55:27 -04:00
|
|
|
}
|
|
|
|
|
2020-12-29 19:10:02 -05:00
|
|
|
WX_DECLARE_STRING_HASH_MAP(wxString, wxWebRequestHeaderMap);
|
|
|
|
|
2020-12-12 20:41:01 -05:00
|
|
|
namespace wxPrivate
|
|
|
|
{
|
|
|
|
WXDLLIMPEXP_NET wxString
|
|
|
|
SplitParameters(const wxString& s, wxWebRequestHeaderMap& parameters);
|
|
|
|
}
|
|
|
|
|
2020-12-26 10:43:03 -05:00
|
|
|
TEST_CASE("WebRequestUtils", "[net][webrequest]")
|
2018-11-01 12:33:56 -04:00
|
|
|
{
|
|
|
|
wxString value;
|
|
|
|
wxWebRequestHeaderMap params;
|
|
|
|
|
|
|
|
wxString header = "multipart/mixed; boundary=\"MIME_boundary_01234567\"";
|
|
|
|
|
2020-12-12 20:41:01 -05:00
|
|
|
value = wxPrivate::SplitParameters(header, params);
|
2018-11-01 12:33:56 -04:00
|
|
|
REQUIRE( value == "multipart/mixed" );
|
|
|
|
REQUIRE( params.size() == 1 );
|
|
|
|
REQUIRE( params["boundary"] == "MIME_boundary_01234567" );
|
|
|
|
}
|
|
|
|
|
2018-10-24 17:55:27 -04:00
|
|
|
#endif // wxUSE_WEBREQUEST
|