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

IsLocal returns false when hosted through the TestServer #570

Closed
@Tratcher

Description

@Tratcher

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions