Skip to content

Commit

Permalink
读取HttpMessage测试通过
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Nov 13, 2019
1 parent 75d97e9 commit 76c427f
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 4 deletions.
21 changes: 17 additions & 4 deletions NewLife.Core/Http/HttpCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ public class HttpMessage : IMessage
/// <summary>负载数据</summary>
public Packet Payload { get; set; }

/// <summary>请求方法</summary>
public String Method { get; set; }

/// <summary>请求资源</summary>
public String Uri { get; set; }

/// <summary>内容长度</summary>
public Int32 ContentLength { get; set; } = -1;

Expand Down Expand Up @@ -181,11 +187,20 @@ public virtual Boolean ParseHeaders()
var pk = Header;
if (pk == null || pk.Total == 0) return false;

// 请求方法 GET / HTTP/1.1
var dic = new Dictionary<String, String>(StringComparer.OrdinalIgnoreCase);
var ss = pk.ToStr().Split(Environment.NewLine);
foreach (var item in ss)
{
var kv = item.Split(":");
var kv = ss[0].Split(" ");
if (kv != null && kv.Length >= 3)
{
Method = kv[0].Trim();
Uri = kv[1].Trim();
}
}
for (var i = 1; i < ss.Length; i++)
{
var kv = ss[i].Split(":");
if (kv != null && kv.Length >= 2)
{
dic[kv[0].Trim()] = kv[1].Trim();
Expand All @@ -196,8 +211,6 @@ public virtual Boolean ParseHeaders()
// 内容长度
if (dic.TryGetValue("Content-Length", out var str))
ContentLength = str.ToInt();
else
ContentLength = 0;

return true;
}
Expand Down
82 changes: 82 additions & 0 deletions XUnitTest.Core/Http/HttpCodecTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Text;
using NewLife.Data;
using NewLife.Http;
using Xunit;

namespace XUnitTest.Http
{
public class HttpCodecTests
{
[Theory(DisplayName = "读取GET")]
[InlineData("GET /123.html HTTP/1.1\r\nHost: www.newlifex.com\r\n\r\n")]
[InlineData("GET /123.html HTTP/1.1\r\nHost: www.newlifex.com\r\nContent-Length:0\r\n\r\n")]
//[InlineData("GET /123.html\r\nHost: www.newlifex.com\r\n")]
//[InlineData("GET /123.html\r\nHost: www.newlifex.com")]
public void ReadGetMessge(String http)
{
var msg = new HttpMessage();
var rs = msg.Read(http.GetBytes());
Assert.True(rs);

rs = msg.ParseHeaders();
Assert.True(rs);

Assert.Equal("GET", msg.Method);
Assert.Equal("/123.html", msg.Uri);
Assert.Equal("www.newlifex.com", msg.Headers["host"]);
}

[Theory(DisplayName = "读取POST")]
[InlineData("POST /123.ashx HTTP/1.1\r\nHost: www.newlifex.com\r\nContent-Length:9\r\n\r\ncode=abcd")]
[InlineData("POST /123.ashx HTTP/1.1\r\nHost: www.newlifex.com\r\nContent-Length:0\r\n\r\n")]
[InlineData("POST /123.ashx HTTP/1.1\r\nHost: www.newlifex.com\r\n\r\n")]
public void ReadPostMessage(String http)
{
var msg = new HttpMessage();
var rs = msg.Read(http.GetBytes());
Assert.True(rs);

rs = msg.ParseHeaders();
Assert.True(rs);

Assert.Equal("POST", msg.Method);
Assert.Equal("/123.ashx", msg.Uri);
Assert.Equal("www.newlifex.com", msg.Headers["host"]);

var body = msg.Payload;
Assert.NotNull(body);
if (body.Total == 9)
{
var str = body.ToStr();
Assert.Equal("code=abcd", str);

Assert.Equal(body.Total, msg.ContentLength);
}
else
{
if (msg.Headers.ContainsKey("Content-Length"))
Assert.Equal(body.Total, msg.ContentLength);
else
Assert.Equal(-1, msg.ContentLength);
}
}

[Theory(DisplayName = "写入编码")]
[InlineData("GET /123.html HTTP/1.1\r\nHost: www.newlifex.com\r\n\r\n")]
[InlineData("POST /123.ashx HTTP/1.1\r\nHost: www.newlifex.com\r\nContent-Length:9\r\n\r\ncode=abcd")]
public void WriteCodec(String http)
{
var pk = http.GetBytes();
var msg = new HttpMessage();
var rs = msg.Read(pk);
Assert.True(rs);

var codec = new HttpCodec();
var rm = codec.Write(null, msg) as Packet;
Assert.NotNull(rm);
Assert.Equal(http, rm.ToStr());
}
}
}

0 comments on commit 76c427f

Please sign in to comment.