You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mchsamples-.net-core/AlertSoftwareKit/TestConsole/Program.cs

188 lines
7.7 KiB

using AlertWebAPI.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace TestConsole
{
class Program
{
static HttpClient client = new HttpClient() { MaxResponseContentBufferSize = 256000 };
static string baseUri = @"http://alertwebapi20180205014115.azurewebsites.net/";
//static string baseUri = @"http://localhost:51251/";
static string login;
static string password;
static void Main(string[] args)
{
Console.WriteLine("TEST TOKEN");
Console.WriteLine("Enter your login");
login = Console.ReadLine();
Console.WriteLine("Enter your password");
password = Console.ReadLine();
Console.Clear();
Task.Run(() => GetToken());
Console.ReadKey();
Console.WriteLine("\nTEST GETALL");
var items = TestGetAll().Result;
foreach(var item in items)
{
Console.WriteLine(item.Name);
}
Console.WriteLine("\nTEST GETBYID");
var item1 = TestGetById("D61C83CA-1BFB-43EB-A952-C0364D77E40F").Result;
Console.WriteLine(item1.Name);
Console.WriteLine("\nTEST POST");
Task.Run(() => TestPost(new AlertItem
{
Id = new Guid("6E9BE067-8002-418F-8D17-BE08503C451A"),
Name = "Alert 4",
Date = new DateTime(2018, 2, 5, 2, 58, 00),
Decription = "une autre alerte...",
Type = AlertType.Low,
IsManaged = false,
SignaledBy = "Dwight Schrute"
})).ContinueWith((t) =>
{
var item2 = TestGetById("6E9BE067-8002-418F-8D17-BE08503C451A").Result;
Console.WriteLine(item2.Name);
});
Console.ReadKey();
Console.WriteLine("\nTEST PUT");
Task.Run(() => TestPut(Guid.Parse("6E9BE067-8002-418F-8D17-BE08503C451A"), new AlertItem
{
Id = new Guid("6E9BE067-8002-418F-8D17-BE08503C451A"),
Name = "Alert 5",
Date = new DateTime(2018, 2, 5, 2, 58, 00),
Decription = "une autre alerte...",
Type = AlertType.Low,
IsManaged = false,
SignaledBy = "Dwigth Schrute"
})).ContinueWith((t) =>
{
Console.WriteLine();
var itemsU = TestGetAll().Result;
foreach (var item in itemsU)
{
Console.WriteLine(item.Name);
}
});
Console.ReadKey();
Console.WriteLine("\nTEST DELETE");
Task.Run(() => TestDelete(Guid.Parse("6E9BE067-8002-418F-8D17-BE08503C451A")).ContinueWith((t) =>
{
Console.WriteLine();
var itemsU = TestGetAll().Result;
foreach (var item in itemsU)
{
Console.WriteLine(item.Name);
}
}));
Console.ReadKey();
}
private static string Token { get; set; }
private async static Task GetToken()
{
var bodyString = $@"{{username: ""{login}"", password: ""{password}""}}";
var response = await client.PostAsync($"{baseUri}api/token", new StringContent(bodyString, Encoding.UTF8, "application/json"));
Console.WriteLine(response.StatusCode);
var responseString = await response.Content.ReadAsStringAsync();
var responseJson = JObject.Parse(responseString);
Token = (string)responseJson["token"];
}
public static async Task<List<AlertItem>> TestGetAll()
{
var items = new List<AlertItem>();
var requestMessage = new HttpRequestMessage(HttpMethod.Get, $"{baseUri}api/alert");
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
var alertsResponse = await client.SendAsync(requestMessage);
if (HttpStatusCode.OK != alertsResponse.StatusCode) throw new NotSupportedException();
var alertsResponseString = await alertsResponse.Content.ReadAsStringAsync();
var alertsResponseJson = JArray.Parse(alertsResponseString);
items = JsonConvert.DeserializeObject<List<AlertItem>>(alertsResponseString);
return items;
}
public static async Task<AlertItem> TestGetById(string id)
{
var requestMessage = new HttpRequestMessage(HttpMethod.Get, $"{baseUri}api/alert/{id}");
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
var alertResponse = await client.SendAsync(requestMessage);
if (HttpStatusCode.OK != alertResponse.StatusCode) throw new NotSupportedException();
var alertResponseString = await alertResponse.Content.ReadAsStringAsync();
var item = JsonConvert.DeserializeObject<AlertItem>(alertResponseString);
return item;
}
public static async Task TestPost(AlertItem item)
{
var json = JsonConvert.SerializeObject(item);
//var response = await client.PostAsync($"{baseUri}api/alert", new StringContent(json, Encoding.UTF8, "application/json"));
//if (HttpStatusCode.OK != response.StatusCode) throw new NotSupportedException();
var requestMessage = new HttpRequestMessage(HttpMethod.Post, $"{baseUri}api/alert");
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
requestMessage.Content = new StringContent(json, Encoding.UTF8, "application/json");
var alertResponse = await client.SendAsync(requestMessage);
if (HttpStatusCode.Created != alertResponse.StatusCode) throw new NotSupportedException();
}
public static async Task TestPut(Guid id, AlertItem updatedItem)
{
var json = JsonConvert.SerializeObject(updatedItem);
//var response = await client.PutAsync($"{baseUri}api/alert/{id}", new StringContent(json, Encoding.UTF8, "application/json"));
//if (HttpStatusCode.OK != response.StatusCode) throw new NotSupportedException();
var requestMessage = new HttpRequestMessage(HttpMethod.Put, $"{baseUri}api/alert/{id}");
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
requestMessage.Content = new StringContent(json, Encoding.UTF8, "application/json");
var alertResponse = await client.SendAsync(requestMessage);
if (HttpStatusCode.NoContent != alertResponse.StatusCode) throw new NotSupportedException();
}
public static async Task TestDelete(Guid id)
{
//var response = await client.DeleteAsync($"{baseUri}api/alert/{id}");
//if (HttpStatusCode.OK != response.StatusCode) throw new NotSupportedException();
var requestMessage = new HttpRequestMessage(HttpMethod.Delete, $"{baseUri}api/alert/{id}");
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
var alertResponse = await client.SendAsync(requestMessage);
if (HttpStatusCode.NoContent != alertResponse.StatusCode) throw new NotSupportedException();
}
}
}