comparison OrthancFramework/UnitTestsSources/RestApiTests.cpp @ 4148:732ad6c618ba

removing ChunkedBuffer::AddChunkDestructive()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 18 Aug 2020 16:56:33 +0200
parents bf7b9edf6b81
children b56f3a37a4a1
comparison
equal deleted inserted replaced
4147:ecc11c232326 4148:732ad6c618ba
144 144
145 145
146 TEST(RestApi, ChunkedBuffer) 146 TEST(RestApi, ChunkedBuffer)
147 { 147 {
148 ChunkedBuffer b; 148 ChunkedBuffer b;
149 //b.SetTrailingBufferSize(0); // TODO
150
149 ASSERT_EQ(0u, b.GetNumBytes()); 151 ASSERT_EQ(0u, b.GetNumBytes());
150 152
151 b.AddChunk("hello", 5); 153 b.AddChunk("hello", 5);
152 ASSERT_EQ(5u, b.GetNumBytes()); 154 ASSERT_EQ(5u, b.GetNumBytes());
153 155
156 158
157 std::string s; 159 std::string s;
158 b.Flatten(s); 160 b.Flatten(s);
159 ASSERT_EQ("helloworld", s); 161 ASSERT_EQ("helloworld", s);
160 } 162 }
163
161 164
162 TEST(RestApi, ParseCookies) 165 TEST(RestApi, ParseCookies)
163 { 166 {
164 IHttpHandler::Arguments headers; 167 IHttpHandler::Arguments headers;
165 IHttpHandler::Arguments cookies; 168 IHttpHandler::Arguments cookies;
876 879
877 // New in Orthanc 1.7.2: Allow relative URLs (for DICOMweb in Stone) 880 // New in Orthanc 1.7.2: Allow relative URLs (for DICOMweb in Stone)
878 w.SetUrl("coucou"); 881 w.SetUrl("coucou");
879 w.SetUrl("/coucou"); 882 w.SetUrl("/coucou");
880 } 883 }
884
885
886
887
888 namespace
889 {
890 class TotoBody : public HttpClient::IRequestBody
891 {
892 private:
893 size_t size_;
894 size_t chunkSize_;
895 size_t pos_;
896
897 public:
898 TotoBody(size_t size,
899 size_t chunkSize) :
900 size_(size),
901 chunkSize_(chunkSize),
902 pos_(0)
903 {
904 }
905
906 virtual bool ReadNextChunk(std::string& chunk)
907 {
908 if (pos_ == size_)
909 {
910 return false;
911 }
912
913 chunk.clear();
914 chunk.resize(chunkSize_);
915
916 size_t i = 0;
917 while (pos_ < size_ &&
918 i < chunkSize_)
919 {
920 chunk[i] = '0' + (pos_ % 7);
921 pos_++;
922 i++;
923 }
924
925 if (i < chunk.size())
926 {
927 chunk.erase(i, chunk.size());
928 }
929
930 return true;
931 }
932 };
933
934 class TotoServer : public IHttpHandler
935 {
936 public:
937 virtual bool CreateChunkedRequestReader(std::unique_ptr<IChunkedRequestReader>& target,
938 RequestOrigin origin,
939 const char* remoteIp,
940 const char* username,
941 HttpMethod method,
942 const UriComponents& uri,
943 const Arguments& headers)
944 {
945 return false;
946 }
947
948 virtual bool Handle(HttpOutput& output,
949 RequestOrigin origin,
950 const char* remoteIp,
951 const char* username,
952 HttpMethod method,
953 const UriComponents& uri,
954 const Arguments& headers,
955 const GetArguments& getArguments,
956 const void* bodyData,
957 size_t bodySize)
958 {
959 printf("received %llu\n", bodySize);
960
961 const uint8_t* b = reinterpret_cast<const uint8_t*>(bodyData);
962
963 for (size_t i = 0; i < bodySize; i++)
964 {
965 if (b[i] != ('0' + i % 7))
966 {
967 throw;
968 }
969 }
970
971 output.Answer("ok");
972 return true;
973 }
974 };
975 }
976
977
978 #include "../Sources/HttpServer/HttpServer.h"
979
980 TEST(Toto, DISABLED_Toto)
981 {
982 TotoServer handler;
983 HttpServer server;
984 server.SetPortNumber(5000);
985 server.Register(handler);
986 server.Start();
987
988 WebServiceParameters w;
989 w.SetUrl("http://localhost:5000");
990
991 TotoBody body(600 * 1024 * 1024, 6 * 1024 * 1024 - 17);
992 //TotoBody body(600 * 1024 * 1024, 1);
993
994 HttpClient c(w, "toto");
995 c.SetMethod(HttpMethod_Post);
996 c.AddHeader("Expect", "");
997 c.AddHeader("Transfer-Encoding", "chunked");
998 c.SetBody(body);
999
1000 std::string s;
1001 ASSERT_TRUE(c.Apply(s));
1002
1003 printf(">> [%s]\n", s.c_str());
1004
1005 server.Stop();
1006 }
1007
1008
1009
1010 TEST(Toto, DISABLED_Tata)
1011 {
1012 boost::posix_time::ptime start = boost::posix_time::microsec_clock::local_time();
1013
1014 ChunkedBuffer b;
1015 for (unsigned int i = 0; i < 600 * 1024 * 1024; i++)
1016 {
1017 b.AddChunk("a", 1);
1018 }
1019
1020 boost::posix_time::ptime end = boost::posix_time::microsec_clock::local_time();
1021
1022 printf("time: %d\n", (end-start).total_microseconds());
1023 }