dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.57k stars 10.05k forks source link

Aspnetcore-TestServer -httpclient request is not working in .netcore3.1 unittest #18749

Closed vimala1085 closed 4 years ago

vimala1085 commented 4 years ago

hi, we are migrating from .netcore2.2 to .netcore3.1. During migration in the unit test, httpclient request which is created using TestServer is not hitting the controller. and the response is always404.

Can anybody help me on this to resolve it.

Tratcher commented 4 years ago

Please provide a runnable project that demonstrates the issue.

vimala1085 commented 4 years ago
public class TestStartup
    {
        public TestStartup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }
        public static TestDomainEvent DomainEventTest { get; set; }
        public void ConfigureServices(IServiceCollection services)
         {
            services.AddControllers().AddNewtonsoftJson();
            AddServices(services);
            services.AddHttpContextAccessor();
            services.AddCors();
            services.AddTenantContext();
            //services.AddTransient<HalJsonResultExecutor, HalJsonResultExecutor>();
            services.AddTransient<ITranslationRepository, InMemoryTranslationRepository>();
            services.AddTransient<UAAGroupController, UAAGroupController>();
            services.AddTransient<UAAIdentityProviderController, UAAIdentityProviderController>();
            services.AddTransient<UAAUserController, UAAUserController>();
             services.AddTransient<UAATenantController, UAATenantController>();
            services
                .AddMvcCore()
                .AddFormatterMappings()
                .AddNewtonsoftJson()
                //.AddJsonFormatters()
                .AddFormatterMappings()
                .AddApiExplorer();               
                //.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        }

        public void Configure(IApplicationBuilder app,
                IHostingEnvironment env,
                ILoggerFactory loggerFactory)
           {

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseHttpExceptionMiddleware();
                app.UseCors(builder =>
                    builder.AllowAnyHeader()
                );
            }
            else
            {
                app.UseHttpExceptionMiddleware();
            }
            ConfigureStaticFileHosting(app);            
            app.UseCors(builder =>
                builder.AllowAnyHeader()
            );
            app.UseRouting();
            app.UseAuthentication();
            //  app.UseMvc();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

        }
        private void AddServices(IServiceCollection services)
        {
            IClientCredentialsHelper clientCredentialsHelper = Substitute.For<IClientCredentialsHelper>();
            clientCredentialsHelper.GetToken(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>()).Returns(new TokenResponse() { AccessToken = "testtoken" });
           var _uaaHelperMock = Substitute.For<IUaaHelper>();
            _uaaHelperMock.GetClientCredentialsToken().Returns("test");
            _uaaHelperMock.GetDefaultIdentityProvider().Returns("uaa");
            services.AddSingleton(_uaaHelperMock);
            services.AddSingleton(clientCredentialsHelper);
            IDomainEventsPublisher domainevent = Substitute.For<IDomainEventsPublisher>();
            domainevent.Publish(Arg.Any<object>(), Arg.Any<string>()).Returns(true);
            services.AddSingleton(domainevent);
        }
        private static void ConfigureStaticFileHosting(IApplicationBuilder app)
        {
            var fileOptions = new StaticFileOptions
            {
                ServeUnknownFileTypes = true,
                DefaultContentType = "application/json"
            };

            app.UseStaticFiles(fileOptions);
        }
    }
vimala1085 commented 4 years ago

The below unit test consumes the above Testserver class.

var builder = new WebHostBuilder()
                   .UseEnvironment("Development")
                   .UseKestrel()
                    .CaptureStartupErrors(true)
                   .UseStartup<TestStartup>();
            _server = new TestServer(builder);
  _client = _server.CreateClient();
            _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
            _client.DefaultRequestHeaders.Add("Authorization", "Token");

  **var response =_client.GetAsync("http://localhost:1234/api/users/test").Result;**

in the above get request is not hitting the controller, but actual project is working fine.

vimala1085 commented 4 years ago

The below is the response of the http get request

{StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
}}
    Content: {System.Net.Http.StreamContent}
    Headers: {}
    IsSuccessStatusCode: false
    ReasonPhrase: "Not Found"
    RequestMessage: {Method: GET, RequestUri: 'http://localhost:1234/api/users/test', Version: 1.1, Content: <null>, Headers:
{
  Accept: application/x-www-form-urlencoded
  Authorization: Token
}}
    StatusCode: NotFound
    TrailingHeaders: {}
    Version: {1.1}
vimala1085 commented 4 years ago

can anybody help me on this

Tratcher commented 4 years ago

I don't see anything obvious from the code provided (though you left out the controller). I suggest turning on debug application logging so you can see the logs from routing. Most likely your controllers aren't being discovered in the test environment. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-3.1

ghost commented 4 years ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

See our Issue Management Policies for more information.

voroninp commented 4 years ago

I hit the same issue. I was getting NotFound result until I changed the referenced SDK.

No my test project references Web SDK (tests pass): <Project Sdk="Microsoft.NET.Sdk.Web">

instead of <Project Sdk="Microsoft.NET.Sdk">

Why?! Because RTFM

Full content of test project file:

<Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>

        <IsPackable>false</IsPackable>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="com.luscii.connect.common" Version="11.0.98" />
        <PackageReference Include="com.luscii.connect.common.web.testing" Version="11.0.98" />
        <PackageReference Include="FluentAssertions" Version="5.10.2" />
        <PackageReference Include="Flurl" Version="2.8.2" />
        <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.2" />
        <PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.1.2" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.2" />
        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
        <PackageReference Include="Moq" Version="4.13.1" />
        <PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
        <PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
    </ItemGroup>

    <ItemGroup>
        <ProjectReference Include="..\Connect.Device.Service\Connect.Device.Service.csproj" />
    </ItemGroup>

    <ItemGroup>
      <Folder Include="Properties\" />
    </ItemGroup>

</Project>