|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Net.Http; |
| 3 | +using System.Web.Http; |
| 4 | +using System.Web.Http.Controllers; |
| 5 | +using System.Web.Http.Routing; |
| 6 | +using FluentAssertions; |
| 7 | +using JSONAPI.Http; |
| 8 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 9 | +using Moq; |
| 10 | + |
| 11 | +namespace JSONAPI.Tests.Http |
| 12 | +{ |
| 13 | + [TestClass] |
| 14 | + public class PascalizedControllerSelectorTests |
| 15 | + { |
| 16 | + [TestMethod] |
| 17 | + public void Camelizes_controller_name_without_dashes() |
| 18 | + { |
| 19 | + TestForDefaultString("foo", "Foo"); |
| 20 | + } |
| 21 | + |
| 22 | + [TestMethod] |
| 23 | + public void Camelizes_controller_name_with_underscores() |
| 24 | + { |
| 25 | + TestForDefaultString("foo_bar_baz", "FooBarBaz"); |
| 26 | + } |
| 27 | + |
| 28 | + [TestMethod] |
| 29 | + public void Camelizes_controller_name_with_dots() |
| 30 | + { |
| 31 | + TestForDefaultString("foo.bar.baz", "FooBarBaz"); |
| 32 | + } |
| 33 | + |
| 34 | + [TestMethod] |
| 35 | + public void Camelizes_controller_name_with_dashes() |
| 36 | + { |
| 37 | + TestForDefaultString("foo-bar-baz", "FooBarBaz"); |
| 38 | + } |
| 39 | + |
| 40 | + [TestMethod] |
| 41 | + public void Camelizes_controller_name_with_all_three() |
| 42 | + { |
| 43 | + TestForDefaultString("foo.bar-baz_qux", "FooBarBazQux"); |
| 44 | + } |
| 45 | + |
| 46 | + private void TestForDefaultString(string defaultString, string expectedTransformation) |
| 47 | + { |
| 48 | + // Arrange |
| 49 | + var routeDataDict = new Dictionary<string, object> |
| 50 | + { |
| 51 | + {"controller", defaultString} |
| 52 | + }; |
| 53 | + |
| 54 | + var mockRouteData = new Mock<IHttpRouteData>(MockBehavior.Strict); |
| 55 | + mockRouteData.Setup(m => m.Values).Returns(routeDataDict); |
| 56 | + |
| 57 | + var mockRequestContext = new Mock<HttpRequestContext>(MockBehavior.Strict); |
| 58 | + mockRequestContext.Setup(m => m.RouteData).Returns(mockRouteData.Object); |
| 59 | + |
| 60 | + var request = new HttpRequestMessage(); |
| 61 | + request.SetRequestContext(mockRequestContext.Object); |
| 62 | + |
| 63 | + var mockHttpConfig = new Mock<HttpConfiguration>(MockBehavior.Strict); |
| 64 | + |
| 65 | + var selector = new PascalizedControllerSelector(mockHttpConfig.Object); |
| 66 | + |
| 67 | + // Act |
| 68 | + var actual = selector.GetControllerName(request); |
| 69 | + |
| 70 | + // Assert |
| 71 | + actual.Should().Be(expectedTransformation); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments