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

IsLocal returns false when hosted through the TestServer #576

Closed
fermaem opened this issue Jan 13, 2016 · 6 comments
Closed

IsLocal returns false when hosted through the TestServer #576

fermaem opened this issue Jan 13, 2016 · 6 comments
Assignees

Comments

@fermaem
Copy link

fermaem commented Jan 13, 2016

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)

@troydai
Copy link
Contributor

troydai commented Jan 13, 2016

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

@Tratcher
Copy link
Member

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

@fermaem
Copy link
Author

fermaem commented Jan 13, 2016

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?

@troydai
Copy link
Contributor

troydai commented Jan 13, 2016

@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

This issue was moved to aspnet/Hosting#570

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

3 participants