示例#1
0
TEST(WriteResponse, test_WriteResponse)
{
    HttpResponse response;
    ASSERT_EQ(WriteResponse(-1, response), -1);

    int fd = open("httpResponse.txt", O_WRONLY | O_CREAT);
    response.SetHttpVersion("HTTP/1.0");
    response.SetStatusAndReason("200", "OK");

    string post("POST_BEGIN\r\nPOST_MID\r\n\r\nPOST_END");
    response.SetStatusAndReason("200", "OK");
    response.SetHeaderParam("Content-Length", IntToStr(post.size()));

    // [response] will be responsible for free [data]
    char* data = CreateCStr(post.size() + 1);
    sprintf(data, "%s", post.c_str());
    response.SetBodyAndLen(data, post.size());
    ASSERT_EQ(WriteResponse(fd, response), 0);
    close(fd);

    fd = open("httpResponse.txt", O_RDONLY);
    if (fd < 0)
    {
        ASSERT_TRUE(false);
    }
    char* buf = CreateCStr(BUF_SIZE);
    readn(fd, buf, BUF_SIZE);

    string expected = "HTTP/1.0 200 OK\r\nContent-Length: 32\r\n"
        "\r\n"
        "POST_BEGIN\r\nPOST_MID\r\n\r\nPOST_END";
    ASSERT_STREQ(buf, expected.c_str());
    close(fd);
    delete [] buf;
}
示例#2
0
TEST(SetAndWriteBack, test_SetAndWriteBack_no_msgbody)
{
    int fd = open("./setAndWriteBack.txt", O_WRONLY | O_CREAT);
    if (fd < 0)
    {
        ASSERT_TRUE(false);
        return;
    }

    HttpResponse response;
    response.SetHttpVersion("HTTP/1.0");
    SetAndWriteBack(fd, response, "400", "Bad Request");
    close(fd);

    fd = open("./setAndWriteBack.txt", O_RDONLY);
    char* buf = CreateCStr(BUF_SIZE);
    readn(fd, buf, BUF_SIZE);

    const char* expected = "HTTP/1.0 400 Bad Request\r\n"
        "Content-Length: 0\r\n"
        "Content-Type: text/html\r\n"
        "\r\n";
    ASSERT_STREQ(buf, expected);
}