1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
#include "tut.h"
#include "HttpStatusExtractor.h"
using namespace Passenger;
using namespace std;
namespace tut {
struct HttpStatusExtractorTest {
HttpStatusExtractor ex;
};
DEFINE_TEST_GROUP(HttpStatusExtractorTest);
/* TODO:
* "\r\n" in this test file should really be replaced with "\x0D\x0A".
* So far I haven't countered a platform on which "\r\n" is not equal
* to "\x0D\x0A" but the possibility that they're not equal exists.
*/
TEST_METHOD(1) {
// Status defaults to "200 OK" and buffer is initially empty.
ensure_equals(ex.getStatusLine(), "200 OK\r\n");
ensure_equals(ex.getBuffer(), "");
}
TEST_METHOD(2) {
// Test feeding an entire HTTP response (header + body)
// in 1 pass. The first header is the status line.
const char data[] =
"Status: 201 OK\r\n"
"Content-Type: text/html\r\n"
"\r\n"
"hello world!";
ensure("Parsing completed.", ex.feed(data, sizeof(data) - 1));
ensure_equals("Status was properly extracted.",
ex.getStatusLine(), "201 OK\r\n");
ensure_equals("All data that we've fed so far has been buffered.",
ex.getBuffer(), data);
}
TEST_METHOD(3) {
// Test feeding a single byte initially, and the
// rest of the status line later.
ensure("Parsing is not complete.", !ex.feed("S", 1));
ensure_equals("Status line hasn't changed.",
ex.getStatusLine(), "200 OK\r\n");
ensure_equals("All data that we've fed so far has been buffered.",
ex.getBuffer(), "S");
const char data2[] = "tatus: 300 Abc\r\n";
ensure("Parsing not yet complete.", !ex.feed(data2, sizeof(data2) - 1));
// Parsing completes when full header has been fed.
ensure("Parsing is complete.", ex.feed("\r\n", 2));
ensure_equals("Status line recognized.",
ex.getStatusLine(), "300 Abc\r\n");
ensure_equals("All data that we've fed so far has been buffered.",
ex.getBuffer(), "Status: 300 Abc\r\n\r\n");
}
TEST_METHOD(4) {
// Test feeding an incomplete non-status line, which
// is completed later. The status line is feeded later.
const char data[] = "Content-Type: text/html";
ensure(!ex.feed(data, sizeof(data) - 1));
ensure_equals(ex.getStatusLine(), "200 OK\r\n");
ensure_equals(ex.getBuffer(), data);
const char data2[] = "\r\nStatus: 201 Hello\r\n\r\n";
ensure(ex.feed(data2, sizeof(data2) - 1));
ensure_equals(ex.getStatusLine(), "201 Hello\r\n");
ensure_equals(ex.getBuffer(),
"Content-Type: text/html\r\n"
"Status: 201 Hello\r\n"
"\r\n");
}
TEST_METHOD(5) {
// Test feeding multiple complete lines, none of which
// is the status line. The status line is feeded later.
const char data[] =
"Content-Type: text/html\r\n"
"Foo: bar\r\n";
ensure(!ex.feed(data, sizeof(data) - 1));
ensure_equals(ex.getStatusLine(), "200 OK\r\n");
ensure_equals(ex.getBuffer(), data);
const char data2[] = "Status: 404 Not Found\r\n";
ensure(!ex.feed(data2, sizeof(data2) - 1));
// Parsing completes when full header has been fed.
ensure(ex.feed("\r\n", 2));
ensure_equals(ex.getStatusLine(), "404 Not Found\r\n");
ensure_equals(ex.getBuffer(), string(data) + data2 + "\r\n");
}
TEST_METHOD(6) {
// Test feeding multiple complete lines and a single incomplete line,
// none of which is the status line. The header is completed
// later, but without status line.
const char data[] =
"Content-Type: text/html\r\n"
"Hello: world";
ensure(!ex.feed(data, sizeof(data) - 1));
ensure_equals(ex.getStatusLine(), "200 OK\r\n");
ensure_equals(ex.getBuffer(), data);
const char data2[] = "\r\n\r\nbody data";
ensure(ex.feed(data2, sizeof(data2) - 1));
ensure_equals(ex.getStatusLine(), "200 OK\r\n");
ensure_equals(ex.getBuffer(), string(data) + data2);
}
TEST_METHOD(7) {
// Test feeding an incomplete status line which is larger
// than 3 bytes, which is completed later.
const char data[] = "Status: 500 Internal Se";
ensure(!ex.feed(data, sizeof(data) - 1));
ensure_equals(ex.getStatusLine(), "200 OK\r\n");
ensure_equals(ex.getBuffer(), data);
const char data2[] = "rver Error\r\n\r\n";
ensure(ex.feed(data2, sizeof(data2) - 1));
ensure_equals(ex.getStatusLine(), "500 Internal Server Error\r\n");
ensure_equals(ex.getBuffer(), string(data) + data2);
}
TEST_METHOD(8) {
// Test feeding an entire HTTP response (header + body)
// in 1 pass. There is a status line, but it is NOT the first
// header.
const char data[] =
"Content-Type: text/html\r\n"
"Status: 405 Testing\r\n"
"Hello: world\r\n"
"\r\n"
"bla bla";
ensure(ex.feed(data, sizeof(data) - 1));
ensure_equals(ex.getStatusLine(), "405 Testing\r\n");
ensure_equals(ex.getBuffer(), data);
}
TEST_METHOD(9) {
// Test feeding multiple complete lines and a single incomplete
// line. One of the complete lines is the status line, but it
// is not the first line.
// The response is completed later.
const char data[] =
"Content-Type: text/html\r\n"
"Status: 100 Foo\r\n"
"B";
ensure(!ex.feed(data, sizeof(data) - 1));
ensure_equals(ex.getStatusLine(), "200 OK\r\n");
ensure_equals(ex.getBuffer(), data);
const char data2[] = "la: bla\r\n\r\n";
ensure(ex.feed(data2, sizeof(data2) - 1));
ensure_equals(ex.getStatusLine(), "100 Foo\r\n");
ensure_equals(ex.getBuffer(), string(data) + data2);
}
TEST_METHOD(10) {
// Test feeding multiple complete lines and a single
// incomplete status line. The response is completed
// later
const char data[] =
"Content-Type: text/html\r\n"
"Statu";
ensure(!ex.feed(data, sizeof(data) - 1));
ensure_equals(ex.getStatusLine(), "200 OK\r\n");
ensure_equals(ex.getBuffer(), data);
const char data2[] =
"s: 202 Blabla\r\n"
"Frobnicate: true\r\n"
"\r\n";
ensure(ex.feed(data2, sizeof(data2) - 1));
ensure_equals(ex.getStatusLine(), "202 Blabla\r\n");
ensure_equals(ex.getBuffer(), string(data) + data2);
}
TEST_METHOD(11) {
// If the status in the HTTP data doesn't contain a status text,
// then the status text is added.
const char data[] = "Status: 200\r\n\r\n";
ensure(ex.feed(data, sizeof(data) - 1));
ensure_equals(ex.getStatusLine(), "200 OK\r\n");
}
TEST_METHOD(12) {
// If the status in the HTTP data doesn't contain a status text,
// and the status code is not recognized, then the status text
// "Unknown Status Code" is added.
const char data[] = "Status: 999\r\n\r\n";
ensure(ex.feed(data, sizeof(data) - 1));
ensure_equals(ex.getStatusLine(), "999 Unknown Status Code\r\n");
}
}
|