Skip to content

Instantly share code, notes, and snippets.

@stapetro
Created March 14, 2019 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stapetro/c742d979edc4c5fa04268c3b2840b297 to your computer and use it in GitHub Desktop.
Save stapetro/c742d979edc4c5fa04268c3b2840b297 to your computer and use it in GitHub Desktop.
Unit test exercise on how to test REST API
using System.Net;
using System.Net.Mime;
using NUnit.Framework;
using RestSharp;
namespace UnitTestsExercise
{
[Category("Integration")]
[Category("Rest")]
public class UnitTest1
{
[Test]
public void TestMethodPositive1()
{
var client = new RestClient("https://languagetool.org");
var request = new RestRequest("/api/v2/languages", Method.GET);
var response = client.Execute<LanguageModel>(request);
Assert.That(response, Is.Not.Null, "Response couldn't be deserialized");
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK), "Unexpected HTTP status code");
}
[Test]
public void TestMethodNegative1()
{
var client = new RestClient("https://languagetool.org");
var request = new RestRequest("/api/v2/languages", Method.GET);
var response = client.Execute<LanguageModel>(request);
Assert.That(response, Is.Not.Null, "Response couldn't be deserialized");
Assert.That(response.StatusCode, Is.Not.EqualTo(HttpStatusCode.Forbidden), "Unexpected HTTP status code");
}
[Test]
//Test when text and data are empty
public void WhenRequiredFieldIsEmpty()
{
var client2 = new RestClient("https://languagetool.org");
var request2 = new RestRequest("/api/v2/check", Method.POST);
var response2 = client2.Execute<LanguageModel>(request2);
Assert.That(response2, Is.Not.Null, "Response couldn't be deserialized");
Assert.That(response2.StatusCode, Is.EqualTo(HttpStatusCode.InternalServerError), "Unexpected HTTP status code");
}
[Test]
//Test when text field is not empty and data is empty
public void WhenRequiredFieldIsNotEmpty()
{
var client3 = new RestClient("https://languagetool.org");
var request3 = new RestRequest("/api/v2/check", Method.POST);
request3.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request3.AddHeader("Accept", "application/json");
request3.AddParameter("text", "test");
request3.AddParameter("language", "en-US");
request3.AddParameter("enabledOnly", "false");
var response3 = client3.Execute<LanguageModel>(request3);
Assert.That(response3.Content, Is.Not.Null, "Test");
Assert.That(response3, Is.Not.Null, "Response couldn't be deserialized");
Assert.That(response3.StatusCode, Is.EqualTo(HttpStatusCode.OK), "Unexpected HTTP status code " + response3.StatusCode);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment