-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement additional functionality (#3)
- Loading branch information
1 parent
3e89461
commit ef3aba3
Showing
9 changed files
with
279 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System.Net; | ||
|
||
namespace MockH.Tests | ||
{ | ||
|
||
[TestClass] | ||
public class RedirectTests : ServerTest | ||
{ | ||
|
||
[TestMethod] | ||
public async Task RulesCanRedirectTemporarily() | ||
{ | ||
using var server = MockServer.Run | ||
( | ||
On.Get().Redirect("https://www.google.de") | ||
); | ||
|
||
using var response = await GetAsync(server); | ||
|
||
Assert.AreEqual(HttpStatusCode.TemporaryRedirect, response.StatusCode); | ||
Assert.AreEqual(new Uri("https://www.google.de"), response.Headers.Location); | ||
} | ||
|
||
[TestMethod] | ||
public async Task RulesCanRedirectPermanently() | ||
{ | ||
using var server = MockServer.Run | ||
( | ||
On.Get().Redirect("https://www.google.de", temporary: false) | ||
); | ||
|
||
using var response = await GetAsync(server); | ||
|
||
Assert.AreEqual(HttpStatusCode.Moved, response.StatusCode); | ||
Assert.AreEqual(new Uri("https://www.google.de"), response.Headers.Location); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using GenHTTP.Api.Protocol; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System.Net; | ||
|
||
namespace MockH.Tests | ||
{ | ||
|
||
[TestClass] | ||
public class RespondTests : ServerTest | ||
{ | ||
|
||
[TestMethod] | ||
public async Task BasicResponse() | ||
{ | ||
using var server = MockServer.Run | ||
( | ||
On.Get().Respond(ResponseStatus.InternalServerError) | ||
); | ||
|
||
using var response = await GetAsync(server); | ||
|
||
Assert.AreEqual(HttpStatusCode.InternalServerError, response.StatusCode); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
using GenHTTP.Api.Protocol; | ||
using GenHTTP.Modules.DirectoryBrowsing; | ||
using GenHTTP.Modules.IO; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System.Net; | ||
|
||
namespace MockH.Tests | ||
{ | ||
|
||
[TestClass] | ||
public class RunTests : ServerTest | ||
{ | ||
|
||
[TestMethod] | ||
public async Task TestConstant() | ||
{ | ||
using var server = MockServer.Run | ||
( | ||
On.Get().Run(() => 42) | ||
); | ||
|
||
using var response = await GetAsync(server); | ||
|
||
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); | ||
Assert.AreEqual("42", await response.Content.ReadAsStringAsync()); | ||
} | ||
|
||
private record MyClass(int IntValue, string StringValue); | ||
|
||
[TestMethod] | ||
public async Task TestJson() | ||
{ | ||
using var server = MockServer.Run | ||
( | ||
On.Get().Run(() => new MyClass(42, "The answer")) | ||
); | ||
|
||
using var response = await GetAsync(server); | ||
|
||
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); | ||
Assert.AreEqual("{\"intValue\":42,\"stringValue\":\"The answer\"}", await response.Content.ReadAsStringAsync()); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestQuery() | ||
{ | ||
using var server = MockServer.Run | ||
( | ||
On.Get().Run((int i) => i + 1) | ||
); | ||
|
||
using var response = await GetAsync(server, "/?i=1"); | ||
|
||
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); | ||
Assert.AreEqual("2", await response.Content.ReadAsStringAsync()); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestPath() | ||
{ | ||
using var server = MockServer.Run | ||
( | ||
On.Get("/increment/:i").Run((int i) => i + 1) | ||
); | ||
|
||
using var response = await GetAsync(server, "/increment/1"); | ||
|
||
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); | ||
Assert.AreEqual("2", await response.Content.ReadAsStringAsync()); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestPost() | ||
{ | ||
using var server = MockServer.Run | ||
( | ||
On.Post().Run((MyClass body) => body) | ||
); | ||
|
||
using var response = await PostAsync(server, "{\"intValue\":42,\"stringValue\":\"The answer\"}"); | ||
|
||
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); | ||
Assert.AreEqual("{\"intValue\":42,\"stringValue\":\"The answer\"}", await response.Content.ReadAsStringAsync()); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestStream() | ||
{ | ||
using var server = MockServer.Run | ||
( | ||
On.Post().Run((Stream body) => body.Length) | ||
); | ||
|
||
using var response = await PostAsync(server, "{\"intValue\":42,\"stringValue\":\"The answer\"}"); | ||
|
||
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); | ||
Assert.AreEqual("42" /* :D */, await response.Content.ReadAsStringAsync()); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestRequest() | ||
{ | ||
using var server = MockServer.Run | ||
( | ||
On.Get().Run((IRequest request) => request.Respond().Status(ResponseStatus.BadRequest)) | ||
); | ||
|
||
using var response = await GetAsync(server); | ||
|
||
Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestHandler() | ||
{ | ||
using var server = MockServer.Run | ||
( | ||
On.Get().Run(() => Listing.From(ResourceTree.FromDirectory("./"))) | ||
); | ||
|
||
using var response = await GetAsync(server); | ||
|
||
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters