Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

IsLocal returns false when hosted through the TestServer #570

Closed
Tratcher opened this issue Jan 13, 2016 · 11 comments
Closed

IsLocal returns false when hosted through the TestServer #570

Tratcher opened this issue Jan 13, 2016 · 11 comments
Assignees

Comments

@Tratcher
Copy link
Member

From @fermaem on January 13, 2016 12:47

The following test fails when ran against rc2.

Is this considered as a bug? Wouldn't that be the case, could you please help me working around it (maybe through some additional test configuration setup)?

using System;
using System.Net;
using System.Net.Http;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.TestHost;
using Newtonsoft.Json;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Xunit;

namespace ClassLibrary1
{
    public class Class
    {
        public class Model
        {
            public string RemoteIPAddress;
            public int RemotePort;
            public string LocalIPAddress;
            public int LocalPort;
            public bool IsLocal;
        }

        [Fact]
        public async void tada()
        {
            var builder = new WebApplicationBuilder()
            .Configure(app =>
            {
                app.Run(async context =>
                {
                    var connection = context.Connection;
                    await context.Response.WriteAsync(JsonConvert.SerializeObject(new Model
                    {
                        RemoteIPAddress = connection.RemoteIpAddress?.ToString(),
                        RemotePort = connection.RemotePort,
                        LocalIPAddress = connection.LocalIpAddress?.ToString(),
                        LocalPort = connection.LocalPort,
                        IsLocal = connection.IsLocal
                    }));
                });
            }); ;

            using (var server = new TestServer(builder))
            using (var client = server.CreateClient())
            {
                var request = new HttpRequestMessage(HttpMethod.Get, new Uri("http://localhost/Huh"));
                request.Headers.Host = request.RequestUri.Host;
                var response = await client.SendAsync(request);
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                var body = await response.Content.ReadAsStringAsync();

                var obj = JsonConvert.DeserializeObject<Model>(body);

                Console.WriteLine(body);
                Assert.True(obj.IsLocal);
            }
        }
    }
}

Below what the server returns

{
  "RemoteIPAddress":null,
  "RemotePort":0,
  "LocalIPAddress":null,
  "LocalPort":0,"
  IsLocal":false
}

/cc @troydai (because of #284)

Copied from original issue: aspnet/KestrelHttpServer#576

@Tratcher
Copy link
Member Author

From @troydai on January 13, 2016 19:12

Thanks for reporting the issue. Can I know the implementation of the TestServer? It doesn't seem to be https://github.com/aspnet/KestrelHttpServer/blob/dev/test/Microsoft.AspNet.Server.KestrelTests/TestServer.cs

@Tratcher
Copy link
Member Author

@Tratcher
Copy link
Member Author

This us unrelated to Kestrel. I really want to remove the IsLocal property, it's misleading.

@Tratcher
Copy link
Member Author

From @fermaem on January 13, 2016 20:5

Thanks for the feedback.

Can I know the implementation of the TestServer?

@troydai @Tratcher Indeed this is Microsoft.AspNet.TestHost.TestServer

I really want to remove the IsLocal property, it's misleading.

@Tratcher Any chance you could elaborate further on this?

@Tratcher
Copy link
Member Author

From @troydai on January 13, 2016 22:21

@fermaem this is not an actual Kestrel issue, since the TestServer you're using doesn't use Kestrel instead it initialize the default http components like HttpConnectionFeature (https://github.com/aspnet/HttpAbstractions/blob/dev/src/Microsoft.AspNet.Http/Features/HttpConnectionFeature.cs). Of which the property value of IsLocal is never set, so by default it is False.

For TestServer the assumption is that the server code is aware of the situation that IsLocal property doesn't provide the most accurate information.

@Tratcher
Copy link
Member Author

TestServer Could populate the ips and IsLocal, but with what? Loopbacks?

@troydai
Copy link
Contributor

troydai commented Jan 14, 2016

@fermaem Since we know what cause this issue, I'd like to understand the impact before we come up any solution. In your usage of TestServer, is the IsLocal property so important that it impact your scenario? In the meantime, we don't populate neither address and ports either.

@fermaem
Copy link

fermaem commented Jan 14, 2016

In your usage of TestServer, is the IsLocal property so important that it impact your scenario?

@troydai Thanks for asking.

The initial requirements were:

  • The server refuses to serve request over http (and eventually performs http->https redirections for GET requests)
  • It's ok to allow the server to serve requests over http when
    • being ran on a developer's computer
    • the CI server runs integration tests

We relied on IsLocal to solve this.

@Tratcher
Copy link
Member Author

Ah, we have a different recommendation for those scenarios: Environments. https://github.com/aspnet/Templates/blob/dev/src/BaseTemplates/StarterWeb/Startup.cs#L40-L47.

@fermaem
Copy link

fermaem commented Jan 14, 2016

@Tratcher Makes sense. Considering the requirements, IsLocal wasn't the best approach. I've switched to your much more thoughtful proposal.

Sorry for the noise and thanks a lot for the amazing support ❤️

@troydai
Copy link
Contributor

troydai commented Jan 15, 2016

@fermaem appreciate the feedback, too.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants