2020-08-21 01:47:31 +08:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "test_utils.h"
|
|
|
|
|
2020-09-16 23:51:34 +08:00
|
|
|
class CurlTest : public CurlTestUtils, public ::testing::Test {
|
2020-08-21 01:47:31 +08:00
|
|
|
protected:
|
2020-09-16 23:51:34 +08:00
|
|
|
void SetUp() override {
|
2020-08-28 01:54:00 +08:00
|
|
|
// Start mock server, get port number and check for any error
|
|
|
|
StartMockServer();
|
|
|
|
ASSERT_TRUE(server_thread_.joinable());
|
2020-09-16 23:51:34 +08:00
|
|
|
ASSERT_TRUE(CurlTestSetUp().ok());
|
2020-08-21 01:47:31 +08:00
|
|
|
}
|
|
|
|
|
2020-09-16 23:51:34 +08:00
|
|
|
void TearDown() override {
|
|
|
|
ASSERT_TRUE(CurlTestTearDown().ok());
|
2020-08-28 01:54:00 +08:00
|
|
|
// Detach the server thread
|
|
|
|
server_thread_.detach();
|
2020-08-21 01:47:31 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-16 23:51:34 +08:00
|
|
|
TEST_F(CurlTest, EffectiveUrl) {
|
2020-08-21 01:47:31 +08:00
|
|
|
sapi::v::RemotePtr effective_url_ptr(nullptr);
|
|
|
|
|
2020-09-16 23:51:34 +08:00
|
|
|
ASSERT_TRUE(PerformRequest().ok());
|
2020-08-21 01:47:31 +08:00
|
|
|
|
|
|
|
// Get effective URL
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
int getinfo_code,
|
2020-08-28 01:54:00 +08:00
|
|
|
api_->curl_easy_getinfo_ptr(curl_.get(), CURLINFO_EFFECTIVE_URL,
|
|
|
|
effective_url_ptr.PtrBoth()));
|
2020-08-22 00:08:10 +08:00
|
|
|
ASSERT_EQ(getinfo_code, CURLE_OK);
|
2020-08-21 01:47:31 +08:00
|
|
|
|
|
|
|
// Store effective URL in a string
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(std::string effective_url,
|
2020-08-28 01:54:00 +08:00
|
|
|
sandbox_->GetCString(sapi::v::RemotePtr(
|
2020-08-21 01:47:31 +08:00
|
|
|
effective_url_ptr.GetPointedVar())));
|
|
|
|
|
|
|
|
// Compare effective URL with original URL
|
2020-08-22 00:08:10 +08:00
|
|
|
ASSERT_EQ(effective_url, kUrl);
|
2020-08-21 01:47:31 +08:00
|
|
|
}
|
|
|
|
|
2020-09-16 23:51:34 +08:00
|
|
|
TEST_F(CurlTest, EffectivePort) {
|
2020-08-21 01:47:31 +08:00
|
|
|
sapi::v::Int effective_port;
|
|
|
|
|
2020-09-16 23:51:34 +08:00
|
|
|
ASSERT_TRUE(PerformRequest().ok());
|
2020-08-21 01:47:31 +08:00
|
|
|
|
|
|
|
// Get effective port
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
int getinfo_code,
|
2020-08-28 01:54:00 +08:00
|
|
|
api_->curl_easy_getinfo_ptr(curl_.get(), CURLINFO_PRIMARY_PORT,
|
|
|
|
effective_port.PtrBoth()));
|
2020-08-22 00:08:10 +08:00
|
|
|
ASSERT_EQ(getinfo_code, CURLE_OK);
|
2020-08-21 01:47:31 +08:00
|
|
|
|
|
|
|
// Compare effective port with port set by the mock server
|
2020-08-28 01:54:00 +08:00
|
|
|
ASSERT_EQ(effective_port.GetValue(), port_);
|
2020-08-21 01:47:31 +08:00
|
|
|
}
|
|
|
|
|
2020-09-16 23:51:34 +08:00
|
|
|
TEST_F(CurlTest, ResponseCode) {
|
2020-08-21 01:47:31 +08:00
|
|
|
sapi::v::Int response_code;
|
|
|
|
|
2020-09-16 23:51:34 +08:00
|
|
|
ASSERT_TRUE(PerformRequest().ok());
|
2020-08-21 01:47:31 +08:00
|
|
|
|
|
|
|
// Get response code
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
int getinfo_code,
|
2020-08-28 01:54:00 +08:00
|
|
|
api_->curl_easy_getinfo_ptr(curl_.get(), CURLINFO_RESPONSE_CODE,
|
|
|
|
response_code.PtrBoth()));
|
2020-08-22 00:08:10 +08:00
|
|
|
ASSERT_EQ(getinfo_code, CURLE_OK);
|
2020-08-21 01:47:31 +08:00
|
|
|
|
|
|
|
// Check response code
|
2020-08-22 00:08:10 +08:00
|
|
|
ASSERT_EQ(response_code.GetValue(), 200);
|
2020-08-21 01:47:31 +08:00
|
|
|
}
|
|
|
|
|
2020-09-16 23:51:34 +08:00
|
|
|
TEST_F(CurlTest, ContentType) {
|
2020-08-21 01:47:31 +08:00
|
|
|
sapi::v::RemotePtr content_type_ptr(nullptr);
|
|
|
|
|
2020-09-16 23:51:34 +08:00
|
|
|
ASSERT_TRUE(PerformRequest().ok());
|
2020-08-21 01:47:31 +08:00
|
|
|
|
|
|
|
// Get effective URL
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
int getinfo_code,
|
2020-08-28 01:54:00 +08:00
|
|
|
api_->curl_easy_getinfo_ptr(curl_.get(), CURLINFO_CONTENT_TYPE,
|
|
|
|
content_type_ptr.PtrBoth()));
|
2020-08-22 00:08:10 +08:00
|
|
|
ASSERT_EQ(getinfo_code, CURLE_OK);
|
2020-08-21 01:47:31 +08:00
|
|
|
|
|
|
|
// Store content type in a string
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(std::string content_type,
|
2020-08-28 01:54:00 +08:00
|
|
|
sandbox_->GetCString(sapi::v::RemotePtr(
|
2020-08-21 01:47:31 +08:00
|
|
|
content_type_ptr.GetPointedVar())));
|
|
|
|
|
|
|
|
// Compare content type with "text/plain"
|
2020-08-22 00:08:10 +08:00
|
|
|
ASSERT_EQ(content_type, "text/plain");
|
2020-08-21 01:47:31 +08:00
|
|
|
}
|
|
|
|
|
2020-09-16 23:51:34 +08:00
|
|
|
TEST_F(CurlTest, GETResponse) {
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(std::string response, PerformRequest());
|
2020-08-21 01:47:31 +08:00
|
|
|
|
|
|
|
// Compare response with expected response
|
2020-08-22 00:08:10 +08:00
|
|
|
ASSERT_EQ(response, kSimpleResponse);
|
2020-08-21 01:47:31 +08:00
|
|
|
}
|
|
|
|
|
2020-09-16 23:51:34 +08:00
|
|
|
TEST_F(CurlTest, POSTResponse) {
|
2020-08-21 01:47:31 +08:00
|
|
|
sapi::v::ConstCStr post_fields("postfields");
|
|
|
|
|
|
|
|
// Set request method to POST
|
2020-08-28 01:54:00 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
int setopt_post,
|
|
|
|
api_->curl_easy_setopt_long(curl_.get(), CURLOPT_POST, 1l));
|
2020-08-22 00:08:10 +08:00
|
|
|
ASSERT_EQ(setopt_post, CURLE_OK);
|
2020-08-21 01:47:31 +08:00
|
|
|
|
|
|
|
// Set the size of the POST fields
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
int setopt_post_fields_size,
|
2020-08-28 01:54:00 +08:00
|
|
|
api_->curl_easy_setopt_long(curl_.get(), CURLOPT_POSTFIELDSIZE,
|
|
|
|
post_fields.GetSize()));
|
2020-08-22 00:08:10 +08:00
|
|
|
ASSERT_EQ(setopt_post_fields_size, CURLE_OK);
|
2020-08-21 01:47:31 +08:00
|
|
|
|
|
|
|
// Set the POST fields
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
int setopt_post_fields,
|
2020-08-28 01:54:00 +08:00
|
|
|
api_->curl_easy_setopt_ptr(curl_.get(), CURLOPT_POSTFIELDS,
|
|
|
|
post_fields.PtrBefore()));
|
2020-08-22 00:08:10 +08:00
|
|
|
ASSERT_EQ(setopt_post_fields, CURLE_OK);
|
2020-08-21 01:47:31 +08:00
|
|
|
|
2020-09-16 23:51:34 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(std::string response, PerformRequest());
|
2020-08-21 01:47:31 +08:00
|
|
|
|
|
|
|
// Compare response with expected response
|
2020-08-22 00:08:10 +08:00
|
|
|
ASSERT_EQ(std::string(post_fields.GetData()), response);
|
2020-08-21 01:47:31 +08:00
|
|
|
}
|