Skip to content

Commit

Permalink
Add HeaderResponseWriter,FileResponseWriter,StatusCodeResponseWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Liu committed Jun 15, 2013
1 parent 7d10cd1 commit d28e434
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 10 deletions.
3 changes: 3 additions & 0 deletions Radish.UnitTests/Radish.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SettingTest.cs" />
<Compile Include="UrlTest.cs" />
<Compile Include="Writers\HeaderResponseWriterTests.cs" />
<Compile Include="Writers\ResponseFileWriterTests.cs" />
<Compile Include="Writers\StatusCodeResponseWriterTests.cs" />
<Compile Include="Writers\TextReponseWriterTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
26 changes: 26 additions & 0 deletions Radish.UnitTests/Writers/HeaderResponseWriterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Net;
using NSubstitute;
using NUnit.Framework;
using Radish.Writers;

namespace Radish.UnitTests.Writers
{
public class HeaderResponseWriterTests
{
[Test]
public void Write()
{
// Arrange
var headers = new WebHeaderCollection();
var writer = new HeaderResponseWriter("Content-Type", "text/xml");
var response = Substitute.For<IHttpResponse>();
response.Headers.Returns(headers);

// Act
writer.Write(response);

// Assert
response.Received(1).AppendHeader("Content-Type","text/xml");
}
}
}
3 changes: 1 addition & 2 deletions Radish.UnitTests/Writers/ResponseFileWriterTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.IO;
using System.Text;
using FluentAssertions;
using NSubstitute;
using NUnit.Framework;
Expand All @@ -22,7 +21,7 @@ public void Write()
fileWriter.Write(response);

// Assert
Encoding.UTF8.GetString(responseStream.ToArray()).Should().Be("hello");
responseStream.ToArray().Should().Equal(File.ReadAllBytes("test.txt"));
}
}
}
24 changes: 24 additions & 0 deletions Radish.UnitTests/Writers/StatusCodeResponseWriterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using FluentAssertions;
using NSubstitute;
using NUnit.Framework;
using Radish.Writers;

namespace Radish.UnitTests.Writers
{
public class StatusCodeResponseWriterTests
{
[Test]
public void Write()
{
// Arrange
var writer = new StatusCodeResponseWriter(404);
var response = Substitute.For<IHttpResponse>();

// Act
writer.Write(response);

// Assert
response.StatusCode.Should().Be(404);
}
}
}
28 changes: 28 additions & 0 deletions Radish.UnitTests/Writers/TextReponseWriterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.IO;
using System.Text;
using FluentAssertions;
using NSubstitute;
using NUnit.Framework;
using Radish.Writers;

namespace Radish.UnitTests.Writers
{
public class TextResponseWriterTests
{
[Test]
public void Write()
{
// Arrange
var responseStream = new MemoryStream();
var response = Substitute.For<IHttpResponse>();
response.OutputStream.Returns(responseStream);
var fileWriter = new TextResponseWriter("test", Encoding.UTF8);

// Act
fileWriter.Write(response);

// Assert
responseStream.ToArray().Should().Equal(Encoding.UTF8.GetBytes("test"));
}
}
}
Binary file modified Radish.v11.suo
Binary file not shown.
5 changes: 3 additions & 2 deletions Radish/Radish.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@
<Compile Include="RequestMatcherSetting.cs" />
<Compile Include="RequestSetting.cs" />
<Compile Include="Extractors\UriRequestExtractor.cs" />
<Compile Include="Writers\HeaderResponseWriter.cs" />
<Compile Include="Writers\FileResponseWriter.cs" />
<Compile Include="ResponseHeaderWriter.cs" />
<Compile Include="ResponseSetting.cs" />
<Compile Include="Writers\ResponseStatusCodeWriter.cs" />
<Compile Include="Writers\ResponseTextWriter.cs" />
<Compile Include="Writers\StatusCodeResponseWriter.cs" />
<Compile Include="Writers\TextResponseWriter.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
Expand Down
2 changes: 1 addition & 1 deletion Radish/ResponseSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void Handle(IHttpContext context)

public ResponseHandler Text(string text)
{
_writers.Add(new ResponseTextWriter(text, Encoding.UTF8));
_writers.Add(new TextResponseWriter(text, Encoding.UTF8));
return this;
}

Expand Down
21 changes: 21 additions & 0 deletions Radish/Writers/HeaderResponseWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace Radish.Writers
{
public class HeaderResponseWriter : IResponseWriter
{
private readonly string _name;
private readonly string _value;

public HeaderResponseWriter(string name, string value)
{
_name = name;
_value = value;
}

public void Write(IHttpResponse response)
{
response.AppendHeader(_name, _value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

namespace Radish.Writers
{
public class ResponseStatusCodeWriter : IResponseWriter
public class StatusCodeResponseWriter : IResponseWriter
{
private readonly int _statusCode;

public ResponseStatusCodeWriter(int statusCode)
public StatusCodeResponseWriter(int statusCode)
{
_statusCode = statusCode;
}

public void Write(IHttpResponse response)
{
throw new NotImplementedException();
response.StatusCode = _statusCode;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Radish.Writers
{
public class ResponseTextWriter : IResponseWriter
public class TextResponseWriter : IResponseWriter
{
private readonly string _text;
private readonly Encoding _encoding;


public ResponseTextWriter(string text, Encoding encoding)
public TextResponseWriter(string text, Encoding encoding)
{
_text = text;
_encoding = encoding;
Expand Down

0 comments on commit d28e434

Please sign in to comment.