Tweaks to wxWebRequest::SetData() overload taking stream
Check that the stream is valid, if specified at all, and return false if it isn't -- or if no size was specified and determining stream size failed. Check for SetData() success in the test to provide better diagnostics in case the file it uses is not found (as is the case when running the test from another directory, for example). Also pass wxSharedPtr<> by const reference instead of by value to avoid unnecessary copies.
This commit is contained in:
@@ -58,7 +58,7 @@ public:
|
||||
|
||||
void SetData(const wxString& text, const wxString& contentType, const wxMBConv& conv = wxConvUTF8);
|
||||
|
||||
void SetData(wxSharedPtr<wxInputStream> dataStream, const wxString& contentType, wxFileOffset dataSize = wxInvalidOffset);
|
||||
bool SetData(const wxSharedPtr<wxInputStream>& dataStream, const wxString& contentType, wxFileOffset dataSize = wxInvalidOffset);
|
||||
|
||||
void SetIgnoreServerErrorStatus(bool ignore) { m_ignoreServerErrorStatus = ignore; }
|
||||
|
||||
|
@@ -245,7 +245,9 @@ public:
|
||||
this request.
|
||||
|
||||
@param dataStream
|
||||
The data in this stream will be posted as the request body
|
||||
The data in this stream will be posted as the request body. The
|
||||
stream may be empty, which will result in sending 0 bytes of data,
|
||||
but if not empty, should be valid.
|
||||
@param contentType
|
||||
The value of HTTP "Content-Type" header, e.g.
|
||||
"application/octet-stream".
|
||||
@@ -253,8 +255,11 @@ public:
|
||||
Amount of data which is sent to the server. If set to
|
||||
@c wxInvalidOffset all stream data is sent.
|
||||
|
||||
@return @false if @a dataStream is not-empty but invalid or if @a
|
||||
dataSize is not specified and the attempt to determine stream size
|
||||
failed; @true in all the other cases.
|
||||
*/
|
||||
void SetData(wxSharedPtr<wxInputStream> dataStream,
|
||||
bool SetData(const wxSharedPtr<wxInputStream>& dataStream,
|
||||
const wxString& contentType, wxFileOffset dataSize = wxInvalidOffset);
|
||||
|
||||
/**
|
||||
|
@@ -83,8 +83,11 @@ void wxWebRequest::SetData(const wxString& text, const wxString& contentType, co
|
||||
SetData(wxSharedPtr<wxInputStream>(new wxMemoryInputStream(m_dataText, m_dataText.length())), contentType);
|
||||
}
|
||||
|
||||
void wxWebRequest::SetData(wxSharedPtr<wxInputStream> dataStream, const wxString& contentType, wxFileOffset dataSize)
|
||||
bool wxWebRequest::SetData(const wxSharedPtr<wxInputStream>& dataStream, const wxString& contentType, wxFileOffset dataSize)
|
||||
{
|
||||
if ( !dataStream->IsOk() )
|
||||
return false;
|
||||
|
||||
m_dataStream = dataStream;
|
||||
if ( m_dataStream.get() )
|
||||
{
|
||||
@@ -92,6 +95,9 @@ void wxWebRequest::SetData(wxSharedPtr<wxInputStream> dataStream, const wxString
|
||||
{
|
||||
// Determine data size
|
||||
m_dataSize = m_dataStream->SeekI(0, wxFromEnd);
|
||||
if ( m_dataSize == wxInvalidOffset )
|
||||
return false;
|
||||
|
||||
m_dataStream->SeekI(0);
|
||||
}
|
||||
else
|
||||
@@ -101,6 +107,8 @@ void wxWebRequest::SetData(wxSharedPtr<wxInputStream> dataStream, const wxString
|
||||
m_dataSize = 0;
|
||||
|
||||
SetHeader("Content-Type", contentType);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
wxFileOffset wxWebRequest::GetBytesReceived() const
|
||||
|
@@ -172,8 +172,10 @@ TEST_CASE_METHOD(RequestFixture, "WebRequest", "[net][.]")
|
||||
SECTION("PUT file data")
|
||||
{
|
||||
Create("/put");
|
||||
request->SetData(wxSharedPtr<wxInputStream>(new wxFileInputStream("horse.png")),
|
||||
"image/png");
|
||||
wxSharedPtr<wxInputStream> is(new wxFileInputStream("horse.png"));
|
||||
REQUIRE( is->IsOk() );
|
||||
|
||||
request->SetData(is, "image/png");
|
||||
request->SetMethod("PUT");
|
||||
Run();
|
||||
}
|
||||
|
Reference in New Issue
Block a user