Skip to content

Commit 1721ed7

Browse files
QualityOwlQualityOwl
andauthored
Add REST API Demo Tests (#2)
* Update test config files. * Add REST API test methods. * Clean up formatting in files. * Update README.md file contents --------- Co-authored-by: QualityOwl <josh@qualityowl.llc>
1 parent 197103f commit 1721ed7

15 files changed

Lines changed: 792 additions & 259 deletions

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,29 @@
1-
# demo-rest-api-tests-csharp
1+
# My REST API Tests Project (C#)
2+
3+
A C# test project demonstrating REST API testing against the [Restful Booker](https://restful-booker.herokuapp.com) public API.
4+
5+
## What is tested
6+
7+
Tests cover three areas of the Restful Booker API:
8+
9+
- **Auth** - token generation via `POST /auth`
10+
- **Booking** - create, read, update, and delete bookings via `/booking`
11+
- **Health check** - service availability via `GET /ping`
12+
13+
## Tech stack
14+
15+
- .NET 10 / xUnit
16+
- FluentAssertions
17+
- Newtonsoft.Json
18+
- `HttpClient` via `Microsoft.Extensions.Http`
19+
- `Microsoft.Extensions.Configuration` for settings and secrets
20+
21+
## Configuration
22+
23+
API credentials are read from `Config/appsettings.json` and can be overridden with environment variables (used in CI).
24+
25+
## Running the tests
26+
27+
```bash
28+
dotnet test
29+
```

Restful.Booker.Api/Restful.Booker.Api.Tests/test-cases/AuthController_Tests.cs renamed to Restful.Booker.Api/Restful.Booker.Api.Tests/Controllers/AuthController_Tests.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public async Task POST_auth_ValidCredentials_ReturnsToken()
4242
}
4343

4444
[Fact]
45-
public async Task POST_auth_InvalidCredentials_Returns400BadRequest()
45+
public async Task POST_auth_InvalidCredentials_Returns200WithReasonMessage()
4646
{
4747
// Arrange
4848
var authRequest = new
@@ -60,8 +60,27 @@ public async Task POST_auth_InvalidCredentials_Returns400BadRequest()
6060
var response = await _fixture.HttpClient.PostAsync("/auth", content);
6161
var responseBody = await response.Content.ReadAsStringAsync();
6262

63-
// Assert
64-
response.IsSuccessStatusCode.Should().BeTrue(); // API returns 200 even for bad credentials
63+
// Assert - API returns 200 with a reason field instead of a token
64+
response.IsSuccessStatusCode.Should().BeTrue();
65+
responseBody.Should().Contain("reason");
66+
responseBody.Should().Contain("Bad credentials");
67+
}
68+
69+
[Fact]
70+
public async Task POST_auth_MissingCredentials_Returns200WithReasonMessage()
71+
{
72+
// Arrange - send empty body
73+
var content = new StringContent(
74+
"{}",
75+
Encoding.UTF8,
76+
"application/json");
77+
78+
// Act
79+
var response = await _fixture.HttpClient.PostAsync("/auth", content);
80+
var responseBody = await response.Content.ReadAsStringAsync();
81+
82+
// Assert - API returns 200 with a reason field instead of a token
83+
response.IsSuccessStatusCode.Should().BeTrue();
6584
responseBody.Should().Contain("reason");
6685
}
67-
}
86+
}

0 commit comments

Comments
 (0)