|
| 1 | + |
| 2 | +#include "handler/upload_handler.hpp" |
| 3 | +#include "http/http_response.hpp" |
| 4 | +#include "stdio.h" |
| 5 | +#include "utest/utest.h" |
| 6 | + |
| 7 | +#include <iostream> |
| 8 | + |
| 9 | +static void write_all(Handler* handler, const std::string& content) |
| 10 | +{ |
| 11 | + size_t written = 0; |
| 12 | + |
| 13 | + while (handler->needs_input()) { |
| 14 | + size_t n = handler->write_data(content.c_str() + written, content.size() - written); |
| 15 | + written += n; |
| 16 | + |
| 17 | + if ((n == 0 && handler->needs_input())) |
| 18 | + break; |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +UTEST(UploadHandlerTest, needs_input_upload_ongoing) |
| 23 | +{ |
| 24 | + std::string upload_path = "www/example/needs_input_upload_ongoing.txt"; |
| 25 | + std::string content = "some content to be written to the file"; |
| 26 | + Handler* handler = new UploadHandler(upload_path, content.size()); |
| 27 | + ASSERT_TRUE(handler->needs_input()); |
| 28 | + delete (handler); |
| 29 | + remove(upload_path.c_str()); |
| 30 | +} |
| 31 | + |
| 32 | +UTEST(UploadHandlerTest, has_output_upload_ongoing) |
| 33 | +{ |
| 34 | + std::string upload_path = "www/example/has_output_upload_ongoing.txt"; |
| 35 | + std::string content = "some content to be written to the file"; |
| 36 | + Handler* handler = new UploadHandler(upload_path, content.size()); |
| 37 | + ASSERT_FALSE(handler->has_output()); |
| 38 | + delete (handler); |
| 39 | + remove(upload_path.c_str()); |
| 40 | +} |
| 41 | + |
| 42 | +UTEST(UploadHandlerTest, needs_input_upload_done) |
| 43 | +{ |
| 44 | + std::string upload_path = "www/example/needs_input_upload_done.txt"; |
| 45 | + std::string content = "some content to be written to the file"; |
| 46 | + Handler* handler = new UploadHandler(upload_path, content.size()); |
| 47 | + write_all(handler, content); |
| 48 | + ASSERT_TRUE(handler->has_output()); |
| 49 | + delete (handler); |
| 50 | + remove(upload_path.c_str()); |
| 51 | +} |
| 52 | + |
| 53 | +UTEST(UploadHandlerTest, has_output_upload_done) |
| 54 | +{ |
| 55 | + std::string upload_path = "www/example/has_output_upload_done.txt"; |
| 56 | + std::string content = "some content to be written to the file"; |
| 57 | + Handler* handler = new UploadHandler(upload_path, content.size()); |
| 58 | + write_all(handler, content); |
| 59 | + ASSERT_TRUE(handler->has_output()); |
| 60 | + delete (handler); |
| 61 | + remove(upload_path.c_str()); |
| 62 | +} |
| 63 | + |
| 64 | +UTEST(UploadHandlerTest, check_content_uploaded) |
| 65 | +{ |
| 66 | + std::string upload_path = "www/example/check_content_uploaded.txt"; |
| 67 | + std::string content = "some content to be written to the file"; |
| 68 | + Handler* handler = new UploadHandler(upload_path, content.size()); |
| 69 | + write_all(handler, content); |
| 70 | + ASSERT_FALSE(handler->needs_input()); |
| 71 | + ASSERT_TRUE(handler->has_output()); |
| 72 | + delete (handler); |
| 73 | + |
| 74 | + // validating content |
| 75 | + FILE* f = fopen(upload_path.c_str(), "r"); |
| 76 | + std::cout << "opening the file" << std::endl; |
| 77 | + ASSERT_TRUE(f != NULL); |
| 78 | + char buffer[1028]; |
| 79 | + size_t n = fread(buffer, 1, sizeof(buffer), f); |
| 80 | + fclose(f); |
| 81 | + std::string file_content(buffer, n); |
| 82 | + std::cout << "file_content = " << file_content << std::endl; |
| 83 | + ASSERT_STREQ(content.c_str(), file_content.c_str()); |
| 84 | + remove(upload_path.c_str()); |
| 85 | +} |
| 86 | + |
| 87 | +UTEST(UploadHandlerTest, ReturnsUploadSuccess) |
| 88 | +{ |
| 89 | + std::string upload_path = "www/example/return_201.txt"; |
| 90 | + std::string content = "some content to be written to the file"; |
| 91 | + Handler* handler = new UploadHandler(upload_path, content.size()); |
| 92 | + ASSERT_FALSE(handler->has_output()); |
| 93 | + write_all(handler, content); |
| 94 | + ASSERT_TRUE(handler->has_output()); |
| 95 | + char buffer[1028]; |
| 96 | + size_t n = handler->read_data(buffer, sizeof(buffer)); |
| 97 | + std::string http_res(buffer, n); |
| 98 | + ASSERT_TRUE(http_res.find("201") != std::string::npos); |
| 99 | + delete (handler); |
| 100 | + remove(upload_path.c_str()); |
| 101 | +} |
| 102 | + |
| 103 | +UTEST(UploadHandlerTest, ReturnsFileAlreadyExists) |
| 104 | +{ |
| 105 | + std::string upload_path = "www/example/return_409.txt"; |
| 106 | + std::string content = "some content to be written to the file"; |
| 107 | + { |
| 108 | + Handler* handler1 = new UploadHandler(upload_path, content.size()); |
| 109 | + write_all(handler1, content); |
| 110 | + ASSERT_TRUE(handler1->has_output()); |
| 111 | + delete handler1; |
| 112 | + } |
| 113 | + Handler* handler2 = new UploadHandler(upload_path, content.size()); |
| 114 | + ASSERT_TRUE(handler2->has_output()); |
| 115 | + char buffer[1028]; |
| 116 | + size_t n = handler2->read_data(buffer, sizeof(buffer)); |
| 117 | + std::string http_res(buffer, n); |
| 118 | + ASSERT_TRUE(http_res.find("409") != std::string::npos); |
| 119 | + delete (handler2); |
| 120 | + remove(upload_path.c_str()); |
| 121 | +} |
| 122 | + |
| 123 | +UTEST(UploadHandlerTest, return_507) |
| 124 | +{ |
| 125 | + UTEST_SKIP("TODO: Test Disk full without making your machine explode"); |
| 126 | +} |
0 commit comments