<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using James.Testing.Rest.IntegrationTests.Models;
using Nancy;
using NUnit.Framework;
using HttpStatusCode = System.Net.HttpStatusCode;

namespace James.Testing.Rest.IntegrationTests.RequestTests
{
    [TestFixture]
    public class GetTests : HostTestFixture
    {
        [Test]
        public void given_uri_for_existing_resource_when_getting_should_return_resource()
        {
            Request
                .Get<IList<Person>>(GetUriString(GetModule.PeopleResource))
                .Verify("Body != null", r => r.Body != null)
                .Verify("Count == 2", r => r.Body.Count == 2)
                .Verify("Item 1, FirstName = Todd", r => r.Body[0].FirstName == "Todd")
                .Verify("Item 1, LastName = Meinershagen", r => r.Body[0].LastName == "Meinershagen")
                .Verify("Item 2, FirstName = Brian", r => r.Body[1].FirstName == "Brian")
                .Verify("Item 2, LastName = Ellis", r => r.Body[1].LastName == "Ellis")
                .Verify("StatusCode == OK", r => r.StatusCode == HttpStatusCode.OK);
        }

        [Test]
        public void given_uri_for_non_existing_resource_when_getting_should_return_with_not_found_status()
        {
            Request
                .Get<object>(GetUriString("DoesNotExist"))
                .Verify("Body == null", r => r.Body == null)
                .VerifyThat(r => r.StatusCode).Is(HttpStatusCode.NotFound);
        }

        [Test]
        public void given_uri_for_existing_resource_with_headers_when_getting_should_return_resource()
        {
            Request
                .Get<Dictionary<string, string>>(GetUriString(GetModule.HeadersResource),
                    new {Id = "1;1", Test = "verify", x_medassets_auth = "test"})
                .Verify(x => x.Body["id"] == "1;1")
                .Verify(x => x.Body["test"] == "verify")
                .Verify(x => x.Body["x-medassets-auth"] == "test");
        }

        [Test]
        public void given_uri_for_existing_resource_when_getting_as_dynamic_should_return_resource()
        {
            Request
                .GetAsDynamic(GetUriString(GetModule.DynamicResource))
                .Verify(x => x.Body.firstName == "Todd")
                .Verify(x => x.Body.lastName == "Meinershagen");
        }
    }

    [TestFixture]
    public class given_resource_when_getting_as_bytes : HostTestFixture
    {
        [TestFixtureSetUp]
        public void SetUp()
        {
            Request
                .GetAsBytes(GetUriString(GetModule.DocumentsResource) + "/" + Guid.NewGuid());
        }

        [Test]
        public void should_return_bytes()
        {
            Request
                .CurrentResponse<byte[]>()
                .Verify(r => r.Body.Length > 0);
        }

        [Test]
        public void should_be_ok_status()
        {
            Request
                .CurrentResponse<byte[]>()
                .Verify(r => r.StatusCode == HttpStatusCode.OK);
        }

        [Test]
        public void should_return_headers()
        {
            Request
                .CurrentResponse<byte[]>()
                .Verify(r => r.Headers.Any());
        }
    }

    public class GetModule : NancyModule
    {
        public const string PeopleResource = "People";
        public const string DocumentsResource = "Documents";
        public const string HeadersResource = "Headers";
        public const string DynamicResource = "Dynamic";

        private readonly Person[] _people = new[]
        {
            new Person { Id = Guid.NewGuid(), FirstName = "Todd", LastName = "Meinershagen" },
            new Person { Id = Guid.NewGuid(), FirstName = "Brian", LastName = "Ellis" }
        };

        public GetModule()
        {
            Get[PeopleResource] = _ => Negotiate
                .WithModel(_people)
                .WithStatusCode(Nancy.HttpStatusCode.OK);

            Get[DynamicResource] = _ => Negotiate
                .WithModel(new {FirstName = "Todd", LastName = "Meinershagen"});

            Get[HeadersResource] = _ =>
            {
                var headers = Request.Headers.ToDictionary(header => header.Key, header => header.Value.First());
                return Negotiate
                    .WithModel(headers);
            };

            Get[DocumentsResource + "/{Id}"] = _ =><commit_msg>added some header tests for get requests.<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using James.Testing.Rest.IntegrationTests.Models;
using Nancy;
using NUnit.Framework;
using HttpStatusCode = System.Net.HttpStatusCode;

namespace James.Testing.Rest.IntegrationTests.RequestTests
{
    [TestFixture]
    public class GetTests : HostTestFixture
    {
        [Test]
        public void given_uri_for_existing_resource_when_getting_should_return_resource()
        {
            Request
                .Get<IList<Person>>(GetUriString(GetModule.PeopleResource))
                .Verify("Body != null", r => r.Body != null)
                .Verify("Count == 2", r => r.Body.Count == 2)
                .Verify("Item 1, FirstName = Todd", r => r.Body[0].FirstName == "Todd")
                .Verify("Item 1, LastName = Meinershagen", r => r.Body[0].LastName == "Meinershagen")
                .Verify("Item 2, FirstName = Brian", r => r.Body[1].FirstName == "Brian")
                .Verify("Item 2, LastName = Ellis", r => r.Body[1].LastName == "Ellis")
                .Verify("StatusCode == OK", r => r.StatusCode == HttpStatusCode.OK);
        }

        [Test]
        public void given_uri_for_non_existing_resource_when_getting_should_return_with_not_found_status()
        {
            Request
                .Get<object>(GetUriString("DoesNotExist"))
                .Verify("Body == null", r => r.Body == null)
                .VerifyThat(r => r.StatusCode).Is(HttpStatusCode.NotFound);
        }

        [Test]
        public void given_uri_for_existing_resource_with_headers_when_getting_should_return_resource_properly()
        {
            Request
                .Get<Dictionary<string, string>>(GetUriString(GetModule.HeadersResource),
                    new {Id = "1;1", Test = "verify", x_medassets_auth = "test"})
                .VerifyThat(r => r.StatusCode).Is(HttpStatusCode.OK)
                .Verify(r => r.Body["id"] == "1;1")
                .Verify(r => r.Body["test"] == "verify")
                .Verify(r => r.Body["x-medassets-auth"] == "test")
                .Verify(r => r.Headers.GetValues("id").First() == "1;1")
                .Verify(r => r.Headers.GetValues("test").First() == "verify")
                .Verify(r => r.Headers.GetValues("x-medassets-auth").First() == "test");
        }

        [Test]
        public void given_uri_for_existing_resource_when_getting_as_dynamic_should_return_resource()
        {
            Request
                .GetAsDynamic(GetUriString(GetModule.DynamicResource))
                .Verify(x => x.Body.firstName == "Todd")
                .Verify(x => x.Body.lastName == "Meinershagen");
        }

        [Test]
        public void given_uri_for_existing_resource_with_headers_when_getting_as_dynamic_should_return_resource()
        {
            Request
                .GetAsDynamic(GetUriString(GetModule.HeadersResource),
                    new {Id = "1", FirstName = "Tammy", LastName = "Bennett"})
                .Verify(r => r.Body.id == "1")
                .Verify(r => r.Body.firstName == "Tammy")
                .Verify(r => r.Body.lastName == "Bennett")
                .Verify(r => r.Headers.GetValues("id").First() == "1")
                .Verify(r => r.Headers.GetValues("firstName").First() == "Tammy")
                .Verify(r => r.Headers.GetValues("lastName").First() == "Bennett");
        }

        [Test]
        public void given_uri_for_existing_resource_when_getting_as_bytes_should_return_resource()
        {
            Request
                .GetAsDynamic(GetUriString(GetModule.HeadersResource),
                    new { Id = "1", FirstName = "Tammy", LastName = "Bennett" })
                .Verify(r => r.Headers.GetValues("id").First() == "1")
                .Verify(r => r.Headers.GetValues("firstName").First() == "Tammy")
                .Verify(r => r.Headers.GetValues("lastName").First() == "Bennett");
        }

    }

    [TestFixture]
    public class given_existing_resource_when_getting_as_bytes : HostTestFixture
    {
        [TestFixtureSetUp]
        public void SetUp()
        {
            Request
                .GetAsBytes(GetUriString(GetModule.DocumentsResource) + "/" + Guid.NewGuid());
        }

        [Test]
        public void should_return_bytes()
        {
            Request
                .CurrentResponse<byte[]>()
                .Verify(r => r.Body.Length > 0);
        }

        [Test]
        public void should_be_ok_status()
        {
            Request
                .CurrentResponse<byte[]>()
                .Verify(r => r.StatusCode == HttpStatusCode.OK);
        }

        [Test]
        public void should_return_headers()
        {
            Request
                .CurrentResponse<byte[]>()
                .Verify(r => r.Headers.Any());
        }
    }

    public class GetModule : NancyModule
    {
        public const string PeopleResource = "People";
        public const string DocumentsResource = "Documents";
        public const string HeadersResource = "Headers";
        public const string DynamicResource = "Dynamic";

        private readonly Person[] _people = new[]
        {
            new Person { Id = Guid.NewGuid(), FirstName = "Todd", LastName = "Meinershagen" },
            new Person { Id = Guid.NewGuid(), FirstName = "Brian", LastName = "Ellis" }
        };

        public GetModule()
        {
            Get[PeopleResource] = _ => Negotiate
                .WithModel(_people)
                .WithStatusCode(Nancy.HttpStatusCode.OK);

            Get[DynamicResource] = _ => Negotiate
                .WithModel(new {FirstName = "Todd", LastName = "Meinershagen"});

            Get[HeadersResource] = _ =>
            {
                var headers = Request.Headers.ToDictionary(header => header.Key, header => header.Value.First());

                return Negotiate
                    .WithModel(headers)
                    .WithHeaders(headers.Select(h => new {Header = h.Key, h.Value}).ToArray());
            };

            Get[DocumentsResource + "/{Id}"] = _ =>
{'fraction_non_alphanumeric': 0.12833112833112834, 'fraction_numerical': 0.0034965034965034965, 'mean_word_length': 1.9267146017699115, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6741763', 'n_tokens_mistral': 3065, 'n_tokens_neox': 2962, 'n_words': 533}