Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
file 190 lines (166 sloc) 5.95 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
using System;
using System.Collections.Generic;
using NUnit.Framework;
using ServiceStack.Common.Tests;
using ServiceStack.Text;
using ServiceStack.Validation;
using ServiceStack.WebHost.IntegrationTests.Services;

namespace ServiceStack.WebHost.IntegrationTests.Tests
{
/// <summary>
/// This base class executes all Web Services ignorant of the endpoints its hosted on.
/// The same tests below are re-used by the Unit and Integration TestFixture's declared below
/// </summary>
[TestFixture]
public abstract class WebServicesTests
: TestBase
{
private const string TestString = "ServiceStack";

protected WebServicesTests()
: base(Config.ServiceStackBaseUri, typeof(ReverseService).Assembly)
{
}

protected override void Configure(Funq.Container container) { }

[Test]
public void Does_Execute_ReverseService()
{
var client = CreateNewServiceClient();
var response = client.Send<ReverseResponse>(
new Reverse { Value = TestString });

var expectedValue = ReverseService.Execute(TestString);
Assert.That(response.Result, Is.EqualTo(expectedValue));
}

[Test]
public void Does_Execute_Rot13Service()
{
var client = CreateNewServiceClient();
var response = client.Send<Rot13Response>(new Rot13 { Value = TestString });

var expectedValue = TestString.ToRot13();
Assert.That(response.Result, Is.EqualTo(expectedValue));
}

[Test]
public void Can_Handle_Exception_from_AlwaysThrowService()
{
var client = CreateNewServiceClient();
try
{
var response = client.Send<AlwaysThrowsResponse>(
new AlwaysThrows { Value = TestString });

response.PrintDump();
Assert.Fail("Should throw HTTP errors");
}
catch (WebServiceException webEx)
{
var response = (AlwaysThrowsResponse) webEx.ResponseDto;
var expectedError = AlwaysThrowsService.GetErrorMessage(TestString);
Assert.That(response.ResponseStatus.ErrorCode,
Is.EqualTo(typeof(NotImplementedException).Name));
Assert.That(response.ResponseStatus.Message,
Is.EqualTo(expectedError));
}
}

[Test]
        public void Can_Handle_Exception_from_AlwaysThrowsList_with_GET_route()
        {
            var client = CreateNewServiceClient();
            if (client is WcfServiceClient) return;
            try
            {
                var response = client.Get<List<AlwaysThrows>>("/throwslist/404/{0}".Fmt(TestString));

                response.PrintDump();
                Assert.Fail("Should throw HTTP errors");
            }
            catch (WebServiceException webEx)
            {
                Assert.That(webEx.StatusCode, Is.EqualTo(404));

                var response = (ErrorResponse)webEx.ResponseDto;
                var expectedError = AlwaysThrowsService.GetErrorMessage(TestString);
                Assert.That(response.ResponseStatus.ErrorCode,
                    Is.EqualTo(typeof(NotImplementedException).Name));
                Assert.That(response.ResponseStatus.Message,
                    Is.EqualTo(expectedError));
            }
        }

[Test]
        public void Can_Handle_Exception_from_AlwaysThrowsValidation()
        {
            var client = CreateNewServiceClient();
            try
            {
                var response = client.Send<List<AlwaysThrows>>(
                    new AlwaysThrowsValidation());

                response.PrintDump();
                Assert.Fail("Should throw HTTP errors");
            }
            catch (WebServiceException webEx)
            {
                var response = (ErrorResponse)webEx.ResponseDto;
                var status = response.ResponseStatus;
                Assert.That(status.ErrorCode, Is.EqualTo("NotEmpty"));
                Assert.That(status.Message, Is.EqualTo("'Value' should not be empty."));
                Assert.That(status.Errors[0].ErrorCode, Is.EqualTo("NotEmpty"));
                Assert.That(status.Errors[0].FieldName, Is.EqualTo("Value"));
                Assert.That(status.Errors[0].Message, Is.EqualTo("'Value' should not be empty."));
            }
        }

[Test]
        public void Request_items_are_preserved_between_filters()
        {
            var client = CreateNewServiceClient();
            if (client is DirectServiceClient) return;
            var response = client.Send<RequestItemsResponse>(new RequestItems { });
            Assert.That(response.Result, Is.EqualTo("MissionSuccess"));
        }
    }


/// <summary>
/// Unit tests simulates an in-process ServiceStack host where all services
/// are executed all in-memory so DTO's are not even serialized.
/// </summary>
public class UnitTests : WebServicesTests
{
public UnitTests()
{
AppHost.Container.RegisterValidators(typeof(AlwaysThrowsValidator).Assembly);
            AppHost.LoadPlugin(new ValidationFeature());
}

protected override IServiceClient CreateNewServiceClient()
{
            return new DirectServiceClient(this, AppHost.ServiceController);
        }
}

public class XmlIntegrationTests : WebServicesTests
{
protected override IServiceClient CreateNewServiceClient()
{
return new XmlServiceClient(ServiceClientBaseUri);
}
}

public class JsonIntegrationTests : WebServicesTests
{
protected override IServiceClient CreateNewServiceClient()
{
return new JsonServiceClient(ServiceClientBaseUri);
}
}

public class JsvIntegrationTests : WebServicesTests
{
protected override IServiceClient CreateNewServiceClient()
{
return new JsvServiceClient(ServiceClientBaseUri);
}
}

public class Soap11IntegrationTests : WebServicesTests
{
protected override IServiceClient CreateNewServiceClient()
{
return new Soap11ServiceClient(ServiceClientBaseUri);
}
}

public class Soap12IntegrationTests : WebServicesTests
{
protected override IServiceClient CreateNewServiceClient()
{
return new Soap12ServiceClient(ServiceClientBaseUri);
}
}

}
Something went wrong with that request. Please try again.