Skip to content

Commit

Permalink
Some URL bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenfuqua committed Jul 23, 2024
1 parent 6c190a1 commit b11e704
Showing 1 changed file with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,8 @@ public void GivenUserIsAlreadyAuthorized()
[Given("a POST request is made to {string} with")]
public async Task GivenAPOSTRequestIsMadeToWith(string url, string body)
{
// Prefer that the "url" fragment have a starting slash, but write
// the code so it will work either way.
url = url.StartsWith('/') ? url[1..] : url;
url = addDataPrefixIfNecessary(url);

url = $"data/{url}";
_logger.log.Information(url);
_apiResponse = await _playwrightContext.ApiRequestContext?.PostAsync(url, new() { Data = body })!;
if (_apiResponse.Headers.ContainsKey("location"))
Expand Down Expand Up @@ -183,7 +180,7 @@ public async Task GivenTheSystemHasTheseReferences(string entityType, DataTable
[When("a POST request is made to {string} with")]
public async Task WhenSendingAPOSTRequestToWithBody(string url, string body)
{
url = $"data/{url}";
url = addDataPrefixIfNecessary(url);
_logger.log.Information($"POST url: {url}");
_logger.log.Information($"POST body: {body}");
_apiResponse = await _playwrightContext.ApiRequestContext?.PostAsync(url, new() { Data = body })!;
Expand All @@ -198,7 +195,7 @@ public async Task WhenSendingAPOSTRequestToWithBody(string url, string body)
[When("a POST request is made for dependent resource {string} with")]
public async Task WhenSendingAPOSTRequestForDependentResourceWithBody(string url, string body)
{
url = $"data/{url}";
url = addDataPrefixIfNecessary(url);
_apiResponse = await _playwrightContext.ApiRequestContext?.PostAsync(url, new() { Data = body })!;
if (_apiResponse.Headers.ContainsKey("location"))
{
Expand All @@ -210,7 +207,8 @@ public async Task WhenSendingAPOSTRequestForDependentResourceWithBody(string url
[When("a PUT request is made to {string} with")]
public async Task WhenAPUTRequestIsMadeToWith(string url, string body)
{
url = $"data/{url.Replace("{id}", _id)}";
url = addDataPrefixIfNecessary(url).Replace("{id}", _id);

body = body.Replace("{id}", _id);
_logger.log.Information($"PUT url: {url}");
_logger.log.Information($"PUT body: {body}");
Expand All @@ -220,7 +218,8 @@ public async Task WhenAPUTRequestIsMadeToWith(string url, string body)
[When("a PUT request is made to referenced resource {string} with")]
public async Task WhenAPUTRequestIsMadeToReferencedResourceWith(string url, string body)
{
url = $"data/{url.Replace("{id}", _referencedResourceId)}";
url = addDataPrefixIfNecessary(url).Replace("{id}", _referencedResourceId);

_logger.log.Information(url);
body = body.Replace("{id}", _referencedResourceId);
_logger.log.Information(body);
Expand All @@ -247,29 +246,23 @@ public async Task WhenAPUTRequestIsMadeToReferencedResourceWith(string url, stri
[When("a DELETE request is made to {string}")]
public async Task WhenADELETERequestIsMadeTo(string url)
{
url = $"data/{url.Replace("{id}", _id)}";
url = addDataPrefixIfNecessary(url).Replace("{id}", _id);

_apiResponse = await _playwrightContext.ApiRequestContext?.DeleteAsync(url)!;
}

[When("a DELETE request is made to referenced resource {string}")]
public async Task WhenADELETERequestIsMadeToReferencedResource(string url)
{
url = $"data/{url.Replace("{id}", _referencedResourceId)}";
url = addDataPrefixIfNecessary(url).Replace("{id}", _referencedResourceId);

_apiResponse = await _playwrightContext.ApiRequestContext?.DeleteAsync(url)!;
}

[When("a GET request is made to {string}")]
public async Task WhenAGETRequestIsMadeTo(string url)
{
url = url.Replace("{id}", _id);

if (url.StartsWith("ed-fi"))
{
// If it doesn't start with ed-fi, then assume that this is
// looking for metadata and should not have "data" added to the
// URL.
url = $"data/{url}";
}
url = addDataPrefixIfNecessary(url).Replace("{id}", _id);

_logger.log.Information(url);
_apiResponse = await _playwrightContext.ApiRequestContext?.GetAsync(url)!;
Expand All @@ -278,7 +271,7 @@ public async Task WhenAGETRequestIsMadeTo(string url)
[When("a GET request is made to {string} using values as")]
public async Task WhenAGETRequestIsMadeToUsingValuesAs(string url, Table table)
{
url = $"data/{url}";
url = addDataPrefixIfNecessary(url);
foreach (var row in table.Rows)
{
var value = row["Values"];
Expand Down Expand Up @@ -494,8 +487,6 @@ public void ThenGettingLessSchoolsThanTheTotalCount()
[Then("schools returned")]
public void ThenSchoolsReturned(Table table)
{
var url = $"data/ed-fi/schools?offset=3&limit=5";

var jsonResponse = _apiResponse.TextAsync().Result;
var responseArray = JArray.Parse(jsonResponse);

Expand All @@ -517,5 +508,18 @@ public void ThenSchoolsReturned(Table table)
}

#endregion

private static string addDataPrefixIfNecessary(string input)
{
// Prefer that the "url" fragment have a starting slash, but write
// the code so it will work either way.
input = input.StartsWith('/') ? input[1..] : input;

// If it doesn't start with ed-fi, then assume that this is looking
// for metadata and should not have "data" added to the URL.
input = input.StartsWith("ed-fi") ? $"data/{input}" : input;

return input;
}
}
}

0 comments on commit b11e704

Please sign in to comment.