Skip to content

Commit b18d06d

Browse files
author
imranazad
committed
EC-1107 Disable SSR During TESTS
1 parent c341067 commit b18d06d

2 files changed

Lines changed: 29 additions & 24 deletions

File tree

Comments.Test/IntegrationTests/API/Answers/CreateAnswerAsOrganisationalLead.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ public async Task Create_Answer_As_Lead_Sets_Answer_Fields_Correctly()
4848

4949
//Act
5050
var response = await builder.PostAsync();
51-
var content = await response.Content.ReadAsStringAsync();
52-
Console.WriteLine(content);
5351
response.EnsureSuccessStatusCode();
5452

5553
var responseString = await response.Content.ReadAsStringAsync();

Comments/Startup.cs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,14 @@ public void ConfigureServices(IServiceCollection services)
159159
});
160160

161161
services.AddOptions();
162-
services.AddHttpClient("ssr", client =>
162+
if (!Environment.IsIntegrationTest())
163163
{
164-
client.BaseAddress = new Uri("http://localhost:4000");
165-
});
166-
services.AddHostedService<NodeSsrService>();
167-
164+
services.AddHttpClient("ssr", client =>
165+
{
166+
client.BaseAddress = new Uri("http://localhost:4000");
167+
});
168+
services.AddHostedService<NodeSsrService>();
169+
}
168170
}
169171

170172
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -266,33 +268,38 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
266268
app.UseHttpsRedirection();
267269
}
268270

271+
269272
app.UseEndpoints(endpoints =>
270273
{
271-
endpoints.Map("{*path:nonfile}", async context =>
274+
if (!env.IsIntegrationTest())
272275
{
273-
var httpClientFactory = context.RequestServices.GetRequiredService<IHttpClientFactory>();
274-
var linkGenerator = context.RequestServices.GetRequiredService<LinkGenerator>();
276+
endpoints.Map("{*path:nonfile}", async context =>
277+
{
278+
var httpClientFactory = context.RequestServices.GetRequiredService<IHttpClientFactory>();
279+
var linkGenerator = context.RequestServices.GetRequiredService<LinkGenerator>();
275280

276-
var client = httpClientFactory.CreateClient("ssr");
281+
var client = httpClientFactory.CreateClient("ssr");
277282

278-
var htmlTemplate = await File.ReadAllTextAsync("ClientApp/build/index.html");
283+
var htmlTemplate = await File.ReadAllTextAsync("ClientApp/build/index.html");
279284

280-
var payload = new
281-
{
282-
url = context.Request.Path.ToString(),
283-
origin = $"{context.Request.Scheme}://{context.Request.Host}",
284-
data = SsrDataBuilder.Build(context, htmlTemplate, linkGenerator)
285-
};
285+
var payload = new
286+
{
287+
url = context.Request.Path.ToString(),
288+
origin = $"{context.Request.Scheme}://{context.Request.Host}",
289+
data = SsrDataBuilder.Build(context, htmlTemplate, linkGenerator)
290+
};
286291

287-
var response = await client.PostAsJsonAsync("/render", payload);
292+
var response = await client.PostAsJsonAsync("/render", payload);
288293

289-
var result = await response.Content.ReadFromJsonAsync<SsrResult>();
294+
var result = await response.Content.ReadFromJsonAsync<SsrResult>();
290295

291-
context.Response.StatusCode = result.StatusCode;
292-
context.Response.ContentType = "text/html";
296+
context.Response.StatusCode = result.StatusCode;
297+
context.Response.ContentType = "text/html";
298+
299+
await context.Response.WriteAsync(result.Html);
300+
});
301+
}
293302

294-
await context.Response.WriteAsync(result.Html);
295-
});
296303
endpoints.MapControllerRoute(name: "PublishedRedirectWithoutDocument",
297304
pattern: "consultations/{consultationId:int}",
298305
defaults: new { controller = "Redirect", action = "PublishedRedirectWithoutDocument" });

0 commit comments

Comments
 (0)