Skip to content

Commit

Permalink
remove explicit System.Net.Http reference
Browse files Browse the repository at this point in the history
  • Loading branch information
joao-r-reis committed Jul 27, 2020
1 parent c5573a1 commit f48568d
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 108 deletions.
Expand Up @@ -81,7 +81,6 @@
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="4.3.0" />
<PackageReference Include="System.Runtime.Serialization.Json" Version="4.0.2" />
<PackageReference Include="System.Runtime.Serialization.Primitives" Version="4.1.1" />
<PackageReference Include="System.Net.Http" Version="4.3.2" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="XunitXml.TestLogger" Version="2.1.26" />
</ItemGroup>
Expand Down
Expand Up @@ -19,7 +19,9 @@
using System.IO;
using System.Linq;
using System.Net;
#if NETCOREAPP
using System.Net.Http;
#endif
using System.Threading;
using System.Threading.Tasks;
using Cassandra.Data.Linq;
Expand Down
Expand Up @@ -19,7 +19,6 @@
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
Expand Down Expand Up @@ -102,34 +101,28 @@ public void WaitForWorkers(int expectedWorkers)
{
Trace.TraceInformation("GraphMultiNodeTests: WaitForWorkers");
var master = FindSparkMaster();
var client = new HttpClient();
var client = new TestHttpClient(new Uri(string.Format("http://{0}:7080", master)));
var count = 100;
while (count > 0)
{
var task = client.GetAsync(string.Format("http://{0}:7080", master));
var task = client.SendAsync(TestHttpClient.Get, string.Empty);
task.Wait(5000);
var response = task.Result;
if (response.StatusCode == HttpStatusCode.OK)
var body = task.Result;
var match = Regex.Match(body, "Alive\\s+Workers:.*(\\d+)</li>", RegexOptions.Multiline);
if (match.Success && match.Groups.Count > 1)
{
var content = response.Content.ReadAsStringAsync();
content.Wait();
var body = content.Result;
var match = Regex.Match(body, "Alive\\s+Workers:.*(\\d+)</li>", RegexOptions.Multiline);
if (match.Success && match.Groups.Count > 1)
try
{
try
var workers = int.Parse(match.Groups[1].Value);
if (workers == expectedWorkers)
{
var workers = int.Parse(match.Groups[1].Value);
if (workers == expectedWorkers)
{
return;
}
}
catch
{
// ignored
return;
}
}
catch
{
// ignored
}
}
count--;
Thread.Sleep(500);
Expand Down
192 changes: 192 additions & 0 deletions src/Cassandra.IntegrationTests/TestBase/TestHttpClient.cs
@@ -0,0 +1,192 @@
//
// Copyright (C) DataStax Inc.
//
// 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.

#if NETCOREAPP
using System.Net.Http;
#endif

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;

using Cassandra.IntegrationTests.SimulacronAPI.Models.Converters;

using Newtonsoft.Json;

namespace Cassandra.IntegrationTests.TestBase
{
internal class TestHttpClient
{
private readonly Uri _baseAddress;

public const string Post = "POST";
public const string Put = "PUT";
public const string Get = "GET";
public const string Delete = "DELETE";

public TestHttpClient(Uri baseAddress)
{
_baseAddress = baseAddress;
}

public Task<string> SendAsync(string method, string url)
{
#if NETCOREAPP
return SendAsync(method, url, null);
#endif

#if NETFRAMEWORK
return SendAsync(method, url, null, null);
#endif
}

#if NETCOREAPP
public async Task<T> SendWithJsonAsync<T>(string method, string url, object body)
{
HttpContent content = null;
if (body != null)
{
var bodyStr = GetJsonFromObject(body);
content = new StringContent(bodyStr, Encoding.UTF8, "application/json");
}

var dataStr = await SendAsync(method, url, content).ConfigureAwait(false);
return string.IsNullOrEmpty(dataStr) ? default(T) : JsonConvert.DeserializeObject<T>(dataStr);
}

public async Task<string> SendAsync(string method, string url, HttpContent content)
{
HttpMethod httpMethod;
switch (method)
{
case TestHttpClient.Put:
httpMethod = HttpMethod.Put;
break;

case TestHttpClient.Get:
httpMethod = HttpMethod.Get;
break;

case TestHttpClient.Delete:
httpMethod = HttpMethod.Delete;
break;

case TestHttpClient.Post:
httpMethod = HttpMethod.Post;
break;

default:
throw new ArgumentException($"{method} not recognized.", nameof(method));
}

using (var client = new HttpClient())
{
client.BaseAddress = _baseAddress;
var message = new HttpRequestMessage(httpMethod, url);
if (content != null)
{
message.Content = content;
}
var response = await client.SendAsync(message).ConfigureAwait(false);

if (!response.IsSuccessStatusCode)
{
throw new Exception($"Invalid status code received {response.StatusCode}.{Environment.NewLine}" +
$"{await response.Content.ReadAsStringAsync().ConfigureAwait(false)}");
}

var dataStr = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
return dataStr;
}
}
#endif

#if NETFRAMEWORK

public async Task<T> SendWithJsonAsync<T>(string method, string url, object body)
{
byte[] content = null;
if (body != null)
{
var bodyStr = GetJsonFromObject(body);
content = Encoding.UTF8.GetBytes(bodyStr);
}

var data = await SendAsync(method, url, "application/json", content).ConfigureAwait(false);
return string.IsNullOrEmpty(data)
? default(T)
: JsonConvert.DeserializeObject<T>(data);
}

public async Task<string> SendAsync(string method, string url, string contentType, byte[] content)
{
var request = (HttpWebRequest)WebRequest.Create(_baseAddress + "/" + url);

request.KeepAlive = false;
request.Method = method;

if (content != null)
{
request.ContentType = contentType;
request.ContentLength = content.Length;
using (var dataStream = request.GetRequestStream())
{
dataStream.Write(content, 0, content.Length);
dataStream.Close();
}
}

using (var response = (HttpWebResponse)request.GetResponse())
{
using (var dataStream = response.GetResponseStream())
{
var reader = new StreamReader(dataStream);
var responseFromServer = await reader.ReadToEndAsync().ConfigureAwait(false);
var statusCode = (int)response.StatusCode;
if (statusCode < 200 || statusCode >= 300)
{
throw new Exception($"Invalid status code received {statusCode}.{Environment.NewLine}" +
$"{responseFromServer}");
}

return responseFromServer;
}
}
}

#endif

private static string GetJsonFromObject(object body)
{
var bodyStr = string.Empty;
if (body != null)
{
var jsonSerializerSettings = new JsonSerializerSettings
{
Converters = new List<JsonConverter>
{
new ConsistencyLevelEnumConverter(),
new TupleConverter()
}
};
bodyStr = JsonConvert.SerializeObject(body, jsonSerializerSettings);
}
return bodyStr;
}
}
}

0 comments on commit f48568d

Please sign in to comment.