Implement additional storage targets

This commit is contained in:
Tobias Taschner
2018-10-30 21:54:40 +01:00
parent d4362f4bca
commit c24efa9821
5 changed files with 188 additions and 17 deletions

View File

@@ -22,6 +22,7 @@
#endif // WX_PRECOMP
#include "wx/webrequest.h"
#include "wx/filename.h"
#include "wx/wfstream.h"
// This test requires the URL to an httpbin instance.
@@ -38,7 +39,11 @@
class RequestFixture
{
public:
RequestFixture() { }
RequestFixture()
{
expectedFileSize = 0;
dataSize = 0;
}
void Create(const wxString& subURL)
{
@@ -51,6 +56,7 @@ public:
{
request.reset(wxWebSession::GetDefault().CreateRequest(url));
request->Bind(wxEVT_WEBREQUEST_STATE, &RequestFixture::OnRequestState, this);
request->Bind(wxEVT_WEBREQUEST_DATA, &RequestFixture::OnData, this);
}
void OnRequestState(wxWebRequestEvent& evt)
@@ -59,6 +65,12 @@ public:
{
case wxWebRequest::State_Unauthorized:
case wxWebRequest::State_Completed:
if ( request->GetStorage() == wxWebRequest::Storage_File )
{
wxFileName fn(evt.GetResponseFileName());
REQUIRE( fn.GetSize() == expectedFileSize );
}
wxFALLTHROUGH;
case wxWebRequest::State_Failed:
case wxWebRequest::State_Cancelled:
loop.Exit();
@@ -66,6 +78,12 @@ public:
}
}
void OnData(wxWebRequestEvent& evt)
{
// Count all bytes recieved via data event for Storage_None
dataSize += evt.GetDataSize();
}
void Run(wxWebRequest::State requiredState = wxWebRequest::State_Completed,
int requiredStatus = 200)
{
@@ -79,6 +97,8 @@ public:
wxEventLoop loop;
wxObjectDataPtr<wxWebRequest> request;
wxInt64 expectedFileSize;
wxInt64 dataSize;
};
TEST_CASE_METHOD(RequestFixture, "WebRequest", "[net][.]")
@@ -126,6 +146,25 @@ TEST_CASE_METHOD(RequestFixture, "WebRequest", "[net][.]")
REQUIRE( request->GetResponse()->AsString() == "The quick brown fox jumps over the lazy dog" );
}
SECTION("GET 99KB to file")
{
expectedFileSize = 99 * 1024;
Create(wxString::Format("/bytes/%lld", expectedFileSize));
request->SetStorage(wxWebRequest::Storage_File);
Run();
REQUIRE( request->GetBytesReceived() == expectedFileSize );
}
SECTION("Process 99KB data")
{
int processingSize = 99 * 1024;
Create(wxString::Format("/bytes/%d", processingSize));
request->SetStorage(wxWebRequest::Storage_None);
Run();
REQUIRE( request->GetBytesReceived() == processingSize );
REQUIRE( dataSize == processingSize );
}
SECTION("PUT file data")
{
Create("/put");