// URL に簡単にアクセスするための静的メソッド Response HttpClient::Connect( const std::string & method, const std::string & url, const HeaderMap & header ) { int prefixLength = url.find( "://" ) + 3; // "https://" の長さ int rootLength = url.substr( prefixLength ).find( "/" ); std::string root = url.substr( prefixLength, rootLength ); // "https://(xxxx)/**" の (xxxx) を抽出 std::string endpoint = url.substr( prefixLength + rootLength ); // 上の (xxxx) 以降を抽出 Connection conn = ( url.substr( 0, 5 ) == "https" ) ? HttpClient::CreateHttpsConnection( root ) : HttpClient::CreateHttpConnection( root ); return conn.Request( "GET", endpoint, header ); }
// 簡易接続のテスト TEST( HttpConnectionTest, EasyRequest ) { Response resp = HttpClient::Connect( Method::Get, "http://cacoo.com/" ); EXPECT_EQ( 200, resp.StatusCode() ); } // 複数回のレスポンスのテスト TEST( HttpConnectionTest, MultiRequest ) { EXPECT_NO_THROW( { try { Connection c = HttpClient::CreateHttpConnection( "cacoo.com" ); Response res1st = c.Request( Method::Get, "/" ); Response res2nd = c.Request( Method::Get, "/features" ); EXPECT_EQ( 200, res1st.StatusCode() ); EXPECT_EQ( 200, res2nd.StatusCode() ); Connection c2 = c; Response res3rd = c2.Request( Method::Get, "/" ); EXPECT_EQ( 200, res3rd.StatusCode() ); } catch( const CacoonException & ex ) { std::cout << ex.Info() << std::endl; throw; }
EXPECT_EQ( 200, resp.StatusCode() ); std::cout << resp.Body(); } // HTTPS の複数回リクエスト TEST( HttpsConnectionTest, MultiRequest ) { EXPECT_NO_THROW( { try { const std::string host = "cohama.backlog.jp"; const std::string url = "/LoginDisplay.action;jsessionid=E3E47A1EE360473E09AAB64426083937.h"; Connection conn = HttpClient::CreateHttpsConnection( host ); Response res1st = conn.Request( Method::Get, url ); Response res2nd = conn.Request( Method::Get, url ); conn = HttpClient::CreateHttpsConnection( host ); Response res3rd = conn.Request( Method::Get, url ); EXPECT_EQ( 200, res1st.StatusCode() ); EXPECT_EQ( 200, res2nd.StatusCode() ); EXPECT_EQ( 200, res3rd.StatusCode() ); } catch( const CacoonException & ex ) { std::cout << ex.Info() << std::endl; throw; }