HttpServer/test/test_HttpHeaders.cpp

153 lines
3.9 KiB
C++
Raw Normal View History

2018-02-15 07:09:01 -08:00
#include "catch.hpp"
#include "HttpHeaders.h"
2018-03-09 17:59:28 -08:00
#include "Buffer.h"
2018-02-15 07:09:01 -08:00
#include "string.h"
using Catch::Matchers::Equals;
2018-02-15 07:09:01 -08:00
TEST_CASE("Empty HttpHeaders","[HttpHeaders]") {
HttpHeaders headers;
2018-02-15 07:09:01 -08:00
CHECK(headers.count() == 0);
}
TEST_CASE("Get and set headers","[HttpHeaders]"){
HttpHeaders headers;
// Check that getting non-existant header is a nullptr
CHECK(headers.get("foo") == nullptr);
// Set 'foo: bar' and check it
headers.set("foo","bar");
2018-03-09 16:10:10 -08:00
CHECK_THAT(headers.get("foo"), Equals("bar"));
2018-02-15 07:09:01 -08:00
CHECK(headers.count() == 1);
}
TEST_CASE("Name contains another name", "[HttpHeaders]")
{
HttpHeaders h;
h.set("Accept-Encoding","foo, bar");
CHECK(h.has("Accept") == false);
}
2018-02-21 18:11:41 -08:00
TEST_CASE("Set headers again","[HttpHeaders]"){
2018-02-15 07:09:01 -08:00
HttpHeaders headers;
headers.set("foo","sushi");
headers.set("foo","soy");
2018-02-21 18:11:41 -08:00
CHECK_THAT(headers.get("foo"), Equals("soy"));
CHECK(headers.count() == 1);
}
TEST_CASE("Append headers","[HttpHeaders]")
{
HttpHeaders headers;
headers.set("foo","sushi");
headers.append("foo","soy");
CHECK_THAT(headers.get("foo"), Equals("sushi, soy"));
2018-02-15 07:09:01 -08:00
CHECK(headers.count() == 1);
}
2018-02-21 18:11:41 -08:00
TEST_CASE("Append empty header behaves like set","[HttpHeaders]")
{
HttpHeaders headers;
headers.append("foo","bar");
2018-02-21 18:11:41 -08:00
CHECK_THAT(headers.get("foo"), Equals("bar"));
CHECK(headers.count() == 1);
}
2018-03-09 16:10:10 -08:00
TEST_CASE("Fill headers","[HttpHeaders]") {
2018-02-15 07:09:01 -08:00
HttpHeaders headers;
2018-03-09 16:10:10 -08:00
char name[HTTPHEADERS_NAME_SZ] = "";
strcpy(name, "header");
2018-03-09 16:10:10 -08:00
for (int i = 0; i < HTTPHEADERS_NUM; i++) {
2018-02-15 07:09:01 -08:00
name[6] = 0x41 + i;
headers.set(name,"Foo");
2018-03-09 16:10:10 -08:00
REQUIRE_THAT(headers.get(name), Equals("Foo"));
CHECK(headers.count() == (i+1) );
}
2018-02-15 07:09:01 -08:00
}
TEST_CASE("Header name is case insensitive","[HttpHeaders]") {
HttpHeaders headers;
headers.set("Foo","bar");
CHECK(headers.has("foo"));
2018-03-09 16:10:10 -08:00
}
TEST_CASE("Copy constructor", "[HttpHeaders]")
{
HttpHeaders orig;
orig.set("Foo","bar");
orig.append("Foo","baz");
orig.set("Bar","qux");
REQUIRE(orig.count() == 2);
HttpHeaders copy(orig);
CHECK(copy.has("Foo"));
CHECK_THAT(copy.get("Foo"), Equals("bar, baz"));
CHECK_THAT(copy.get("Bar"), Equals("qux"));
CHECK(copy.count() == 2);
orig.set("Bux","foo");
CHECK(!copy.has("Bux"));
CHECK(copy.count() == 2);
}
2018-03-09 17:59:28 -08:00
TEST_CASE("HttpHeaders printTo", "[HttpHeaders]")
{
HttpHeaders h;
uint8_t buff[100];
Buffer buffer(buff, 100);
buffer.print(h);
CHECK_THAT((char *) buff, Equals(""));
buffer.flush();
h.set("Foo","Bar");
h.set("Bar","baz");
buffer.print(h);
CHECK_THAT((char *) buff, Equals("Foo: Bar\r\nBar: baz\r\n"));
}
TEST_CASE("HttpHeaders find in list", "[HttpHeaders][HttpHeaders::in]")
{
HttpHeaders h;
h.set("Accept-Encoding","gzip, deflate");
CHECK(h.in("Accept-Encoding","gzip") == true);
CHECK(h.in("Accept-Encoding","deflate") == true);
CHECK(h.in("Accept-Encoding","identity") == false);
CHECK(h.in("Accept","gzip") == false); // Accept is not in the headers
}
TEST_CASE("Find matches when a q-factor is provided", "[HttpHeaders]")
{
HttpHeaders h;
h.set("Accept","text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8");
CHECK(h.in("Accept","text/html"));
CHECK(h.in("Accept","application/xhtml+xml"));
CHECK(h.in("Accept","application/xml"));
CHECK(h.in("Accept","*/*"));
}
TEST_CASE("CURL headers", "[HttpHeaders]")
{
HttpHeaders h;
h.set("Host","localhost:8080");
h.set("User-Agent","curl/7.55.1");
h.set("Accept","*/*");
h.set("Content-Length","3");
h.set("Content-Type","application/x-www-form-urlencoded");
CHECK_THAT(h.get("Host"), Equals("localhost:8080"));
CHECK_THAT(h.get("User-Agent"), Equals("curl/7.55.1"));
CHECK_THAT(h.get("Accept"), Equals("*/*"));
CHECK_THAT(h.get("Content-Length"), Equals("3"));
CHECK_THAT(h.get("Content-Type"), Equals("application/x-www-form-urlencoded"));
}