aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/Helpers/ResponseExtensions.cs
blob: 46c9e81dcfb344ce40bdf73923f7e6dc648b84c1 (plain)
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
using Newtonsoft.Json;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Timeline.Models.Http;
using Xunit;

namespace Timeline.Tests.Helpers
{
    public static class ResponseExtensions
    {
        public static void AssertOk(this HttpResponseMessage response)
        {
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        }

        public static void AssertNotFound(this HttpResponseMessage response)
        {
            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
        }

        public static void AssertBadRequest(this HttpResponseMessage response)
        {
            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
        }

        public static async Task AssertIsPutCreated(this HttpResponseMessage response)
        {
            Assert.Equal(HttpStatusCode.Created, response.StatusCode);
            var body = await response.ReadBodyAsJson<CommonResponse>();
            Assert.Equal(CommonPutResponse.CreatedCode, body.Code);
        }

        public static async Task AssertIsPutModified(this HttpResponseMessage response)
        {
            response.AssertOk();
            var body = await response.ReadBodyAsJson<CommonResponse>();
            Assert.Equal(CommonPutResponse.ModifiedCode, body.Code);
        }


        public static async Task AssertIsDeleteDeleted(this HttpResponseMessage response)
        {
            response.AssertOk();
            var body = await response.ReadBodyAsJson<CommonResponse>();
            Assert.Equal(CommonDeleteResponse.DeletedCode, body.Code);
        }

        public static async Task AssertIsDeleteNotExist(this HttpResponseMessage response)
        {
            response.AssertOk();
            var body = await response.ReadBodyAsJson<CommonResponse>();
            Assert.Equal(CommonDeleteResponse.NotExistsCode, body.Code);
        }

        public static async Task<T> ReadBodyAsJson<T>(this HttpResponseMessage response)
        {
            return JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync());
        }
    }
}