Commit ddf9c22c authored by Mathieu Stefani's avatar Mathieu Stefani

Started to add tests for header parsing

parent 1efd9e47
......@@ -74,7 +74,7 @@ Host::parse(const std::string& data) {
port_ = p;
} else {
host_ = data;
port_ = -1;
port_ = 80;
}
}
......
......@@ -7,6 +7,7 @@
#pragma once
#include "mime.h"
#include "net.h"
#include <string>
#include <type_traits>
#include <memory>
......@@ -138,7 +139,7 @@ public:
, port_(-1)
{ }
explicit Host(const std::string& host, int16_t port = -1)
explicit Host(const std::string& host, Net::Port port = 80)
: host_(host)
, port_(port)
{ }
......@@ -147,11 +148,11 @@ public:
void write(std::ostream& os) const;
std::string host() const { return host_; }
int16_t port() const { return port_; }
Net::Port port() const { return port_; }
private:
std::string host_;
int16_t port_;
Net::Port port_;
};
class UserAgent : public Header {
......
add_executable( run_mime_test mime_test.cc )
target_link_libraries(run_mime_test gtest gtest_main net)
add_test( mime_test run_mime_test )
add_executable( run_headers_test headers_test.cc )
target_link_libraries(run_headers_test gtest gtest_main net)
add_test( headers_test run_headers_test )
#include "gtest/gtest.h"
#include "http_headers.h"
using namespace Net::Http;
TEST(headers_test, content_length) {
ContentLength cl;
cl.parse("3495");
ASSERT_EQ(cl.value(), 3495);
}
TEST(headers_test, host) {
Host host;
host.parse("www.w3.org");
ASSERT_EQ(host.host(), "www.w3.org");
ASSERT_EQ(host.port(), 80);
host.parse("localhost:8080");
ASSERT_EQ(host.host(), "localhost");
ASSERT_EQ(host.port(), 8080);
}
TEST(headers_test, user_agent) {
UserAgent ua;
ua.parse("CERN-LineMode/2.15 libwww/2.17b3");
ASSERT_EQ(ua.ua(), "CERN-LineMode/2.15 libwww/2.17b3");
}
TEST(headers_test, content_encoding) {
ContentEncoding ce;
ce.parse("gzip");
ASSERT_EQ(ce.encoding(), Encoding::Gzip);
}
TEST(headers_test, content_type) {
ContentType ct;
ct.parse("text/html; charset=ISO-8859-4");
const auto& mime = ct.mime();
ASSERT_EQ(mime, MIME(Text, Html));
ASSERT_EQ(mime.getParam("charset").getOrElse(""), "ISO-8859-4");
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment