Skip to content

Commit

Permalink
Add tests for Base64OutputStream / StreamTransformer and MultipartStr…
Browse files Browse the repository at this point in the history
…eam / MultiStream
  • Loading branch information
mikee47 committed Mar 24, 2021
1 parent 759aeae commit 3f210f2
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 5 deletions.
5 changes: 3 additions & 2 deletions tests/HostTests/app/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ RESOURCE(abstract_txt, "abstract.txt")
RESOURCE(test_json, "test.json")
RESOURCE(ut_template1_in_rst, "ut_template1.in.rst")
RESOURCE(ut_template1_out1_rst, "ut_template1.out1.rst")
//RESOURCE(ut_template2_in_rst, "ut_template2.in.rst")
//RESOURCE(ut_template2_out_rst, "ut_template2.in.rst")

RESOURCE(image_png, "image.png")
RESOURCE(multipart_result, "multipart-result.txt")

} // namespace Resource
7 changes: 4 additions & 3 deletions tests/HostTests/include/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ DECLARE_FSTR(README_md)
DECLARE_FSTR(unit_testing_rst) // Unmodified file
DECLARE_FSTR(ut_template1_in_rst)
DECLARE_FSTR(ut_template1_out1_rst) // emit_contents = true
//DECLARE_FSTR(ut_template1_out2_rst)
//DECLARE_FSTR(ut_template2_in_rst)
//DECLARE_FSTR(ut_template2_out_rst)

// Multipart streams
DECLARE_FSTR(image_png)
DECLARE_FSTR(multipart_result)

} // namespace Resource
42 changes: 42 additions & 0 deletions tests/HostTests/modules/Stream.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <HostTests.h>
#include <FlashString/TemplateStream.hpp>
#include <Data/Stream/MemoryDataStream.h>
#include <Data/Stream/Base64OutputStream.h>
#include <Network/WebHelpers/base64.h>

DEFINE_FSTR_LOCAL(template1, "Stream containing {var1}, {var2} and {var3}. {} {{}} {{12345")
DEFINE_FSTR_LOCAL(template1_1, "Stream containing value #1, value #2 and {var3}. {} {{}} {{12345")
Expand Down Expand Up @@ -71,6 +73,46 @@ class StreamTest : public TestGroup
REQUIRE(FS_abstract == s);
REQUIRE(strlen(s.c_str()) == s.length());
}

TEST_CASE("Base64OutputStream / StreamTransformer")
{
auto src = new FSTR::Stream(Resource::image_png);
Base64OutputStream base64stream(src);
MemoryDataStream output;
output.copyFrom(&base64stream);
String s;
REQUIRE(output.moveString(s));
s = base64_decode(s);
REQUIRE(Resource::image_png == s);
}

TEST_CASE("MultipartStream / MultiStream")
{
unsigned itemIndex{0};
constexpr const FlashString* items[]{
&template1, &template1_1, &template1_2, &template2, &template2_1,
};
MultipartStream multi([&]() -> MultipartStream::BodyPart {
MultipartStream::BodyPart part;
if(itemIndex < ARRAY_SIZE(items)) {
part.headers = new HttpHeaders;
part.stream = new FSTR::Stream(*items[itemIndex++]);
}
return part;
});

// For testing, hack the boundary value so we can compare it against a reference output
auto boundary = const_cast<char*>(multi.getBoundary());
memcpy(boundary, _F("oALsXuO7vSbrvve"), 16);

MemoryDataStream mem;
size_t copySize = mem.copyFrom(&multi);
debug_i("copySize = %u", copySize);
REQUIRE(int(copySize) == mem.available());
String s;
REQUIRE(mem.moveString(s));
REQUIRE(Resource::multipart_result == s);
}
}

private:
Expand Down
1 change: 1 addition & 0 deletions tests/HostTests/resource/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
multipart-result.txt binary
Binary file added tests/HostTests/resource/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions tests/HostTests/resource/multipart-result.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

--oALsXuO7vSbrvve
Content-Length: 60

Stream containing {var1}, {var2} and {var3}. {} {{}} {{12345
--oALsXuO7vSbrvve
Content-Length: 64

Stream containing value #1, value #2 and {var3}. {} {{}} {{12345
--oALsXuO7vSbrvve
Content-Length: 68

Stream containing value #1, value #2 and [value #3]. {} {{}} {{12345
--oALsXuO7vSbrvve
Content-Length: 82

This text should {disable}not {var1} really {var2:hello} again {enable}be missing.
--oALsXuO7vSbrvve
Content-Length: 28

This text should be missing.
--oALsXuO7vSbrvve--

0 comments on commit 3f210f2

Please sign in to comment.