int64_t checkStreamContent(PContentStream strm, PContentStream strmR) { l_info << L"checkStreamContent("; char buf[10000] = {0}; int64_t pos = 0; int32_t len = 0; while ((len = strmR->read(buf, 0, sizeof(buf))) > 0) { for (int32_t i = 0; i < len; i++) { unsigned b1 = (unsigned)(pos & 0xFF); unsigned b2 = (unsigned)(buf[i] & 0xFF); if (b1 != b2) { std::wstringstream msg; msg << L"Wrong byte at pos=" << pos; TASSERT(msg.str(), b1, b2); } pos++; } } int64_t nbOfBytes = strm->getContentLength(); if (nbOfBytes != -1) { TASSERT(L"#bytes", nbOfBytes, pos); TASSERT(L"contentLength", nbOfBytes, strmR->getContentLength()); } wstring csR = strmR->getContentType(); TASSERT(L"contentType", strm->getContentType(), csR); l_info << L")checkStreamContent=" << pos; return pos; }
void TestUtilHttp::tassert(const char* file, int line, const std::wstring& msg, PContentStream strm, PContentStream strmR) { com::wilutions::test::tassert(file, line, msg + L".contentLength", strm->getContentLength(), strmR->getContentLength()); char buf[2345], bufR[2345]; int32_t len = 0, lenR; while ((lenR = strm->read(bufR, 0, sizeof(bufR))) != -1) { com::wilutions::test::tassert(file, line, msg + L" read lenR != 0", true, lenR != 0); int32_t offs = 0; while ((len = strmR->read(buf, offs, lenR - offs)) != -1) { com::wilutions::test::tassert(file, line, msg + L" read len != 0", true, len != 0); if (len + offs == lenR) break; offs += len; } if (memcmp(buf, bufR, len)) { com::wilutions::test::tassert(file, line, msg + L".content-diff", 0, 1); } } if (strmR->getContentLength() != 0) { com::wilutions::test::tassert(file, line, msg + L".contentType", strm->getContentType(), strmR->getContentType()); } }
/** * This helper function sends a stream with the given number of bytes. * After the stream is sent, it is read back and compared. * @param nbOfBytes Stream length * @param chunked If true, the stream is sent with chunked encoding. If false, the HTTP request is sent with the Content-Length header. */ void internalTestStreams(int64_t nbOfBytes, bool chunked) { l_info << L"internalTestStreams(chunked=" << chunked << L", nbOfBytes=" << nbOfBytes; PContentStream strm(new MyContentStreamBytes(nbOfBytes, chunked)); // send stream PRemoteStreams remote = client->getRemoteStreams(); l_info << L"remote->setImage..."; remote->setImage(strm); l_info << L"remote->setImage OK"; bool checkContent = true; if (checkContent) { // read stream and compare bytes l_info << L"remote->getImage..."; PContentStream strmR = remote->getImage(); l_info << L"remote->getImage OK"; checkStreamContent(strm, strmR); strmR.reset(); } l_info << L")internalTestStreams"; }
BINLINE void BContentStream::copyProperties(const PContentStream& rhs) { if (rhs) { contentType = rhs->getContentType(); contentLength = rhs->getContentLength(); fileName = rhs->getFileName(); attachmentCode = rhs->getAttachmentCode(); targetId = rhs->getTargetId(); } }
BINLINE int32_t BContentStreamWrapper::read(char* buf, int32_t offs, int32_t len) { int32_t ret = -1; if (len < 0) return -1; if (stdStream) { if (stdStream->eof()) return -1; stdStream->read(buf + offs, (size_t)len); ret = (int32_t)stdStream->gcount(); } else { PContentStream strm = ensureStream(); ret = strm->read(buf, offs, len); } return ret; }
int main(int argc, char *argv[]) { qInstallMessageHandler(myMessageOutput); QCoreApplication a(argc, argv); // Task parented to the application so that it // will be deleted by the application. Task *task = new Task(&a); // This will cause the application to exit when // the task signals finished. QObject::connect(task, SIGNAL(finished()), &a, SLOT(quit())); // This will run the task from the application event loop. QTimer::singleShot(0, task, SLOT(run())); int ret = a.exec(); strm.reset(); hget.reset(); client.reset(); return ret; }
/** * Use class BContentStreamFile to transfer a stream. */ void testRemoteStreamsOneFileStream() { l_info << L"testRemoteStreamsOneFileStream("; std::string fileContent = "hello"; BFile file = BFile::createTempFile(L"byps", L".txt"); byps_ptr<ofstream> fos = file.openWrite(); (*fos) << fileContent; fos->close(); PContentStream strm(new BContentStreamWrapper(file)); PRemoteStreams remote = client->getRemoteStreams(); l_info << L"remote->setImage ..."; remote->setImage(strm); l_info << L"remote->setImage OK"; strm.reset(); file.delet(); l_info << L"remote->getImage ..."; PContentStream strmR = remote->getImage(); l_info << L"remote->getImage OK"; //TASSERT(L"Content-Type", "text/plain", strmR->getContentType()); // there is currently no Content-Type detection in BContentStreamFile l_info << L"getContentLength ..."; int64_t contentLengthR = strmR->getContentLength(); l_info << L"getContentLength OK, #=" << contentLengthR; TASSERT(L"wrong content length", fileContent.size(), (size_t)contentLengthR); TASSERT(L"FileName", file.getName(), strmR->getFileName()); l_info << L"read ..."; char buf[1000] = {0}; int32_t len = strmR->read(buf, 0, sizeof(buf)); l_info << L"read OK, len=" << len; TASSERT(L"wrong stream content", fileContent, std::string(buf)); TASSERT(L"FileName", file.getName(), strmR->getFileName()); TASSERT(L"wrong content length", fileContent.size(), (size_t)strmR->getContentLength()); l_info << L")testRemoteStreamsOneFileStream"; }