<commit_before>﻿using System;
using FakeItEasy;
using FluentAssertions;
using Hydra;
using Hydra.Discovery.SupportedClasses;
using Hydra.Discovery.SupportedOperations;
using Hydra.Nancy;
using JsonLD.Entities;
using Nancy;
using Nancy.Responses.Negotiation;
using Nancy.Testing;
using Newtonsoft.Json;
using Xunit;

namespace Lernaean.Hydra.Tests
{
    public class ServingApiDocumentationTests
    {
        private const string ExpectedApiDocPath = "http://hydra.guru/uber/documentation/path";
        private const string ExpectedLinkHeader = "<" + ExpectedApiDocPath + ">; rel=\"" + global::Hydra.Hydra.apiDocumentation + "\"";

        private readonly Browser browser;

        public ServingApiDocumentationTests()
        {
            var rdfTypeProviderPolicy = A.Fake<IRdfTypeProviderPolicy>();
            A.CallTo(() => rdfTypeProviderPolicy.Create(A<Type>._))
                .ReturnsLazily(call => new Uri("http://example.com/class"));

            this.browser = new Browser(
                configurator =>
                {
                    configurator.Module<TestModule>();
                    configurator.Module<HydraApiDocumentationModule>();
                    configurator.Dependency<IHydraDocumentationSettings>(new TestSettings());
                    configurator.Dependency(rdfTypeProviderPolicy);
                    configurator.Dependency(A.Fake<ISupportedPropertyFactory>());
                    configurator.Dependency(A.Fake<ISupportedClassMetaProvider>());
                    configurator.Dependency(A.Fake<ISupportedOperationFactory>());
                    configurator.ApplicationStartupTask<HydraDocumentationStartup>();
                },
                context => context.HostName("hydra.guru"));
        }

        [Fact]
        public async void Should_append_api_doc_header_to_GET_query()
        {
            // when
            var response = await this.browser.Post("test");

            // then
            response.Headers.Should().ContainKey("Link");
            response.Headers["Link"].Should().Be(ExpectedLinkHeader);
        }

        [Fact]
        public async void Should_append_api_doc_header_to_POST_query()
        {
            // when
            var response = await this.browser.Get("test");

            // then
            response.Headers.Should().ContainKey("Link");
            response.Headers["Link"].Should().Be(ExpectedLinkHeader);
        }

        private class TestModule : NancyModule
        {
            public TestModule()
            {
                this.Get("test", _ => "GET test");
                this.Post("test", _ => "POST test");<commit_msg>ensure that docs link doesn't replace other links<commit_after>﻿using System;
using System.Collections.Generic;
using FakeItEasy;
using FluentAssertions;
using Hydra;
using Hydra.Discovery.SupportedClasses;
using Hydra.Discovery.SupportedOperations;
using Hydra.Nancy;
using JsonLD.Entities;
using Nancy;
using Nancy.Responses.Negotiation;
using Nancy.Testing;
using Newtonsoft.Json;
using Xunit;

namespace Lernaean.Hydra.Tests
{
    public class ServingApiDocumentationTests
    {
        private const string ExpectedApiDocPath = "http://hydra.guru/uber/documentation/path";
        private const string ExpectedLinkHeader = "<" + ExpectedApiDocPath + ">; rel=\"" + global::Hydra.Hydra.apiDocumentation + "\"";

        private readonly Browser browser;

        public ServingApiDocumentationTests()
        {
            var rdfTypeProviderPolicy = A.Fake<IRdfTypeProviderPolicy>();
            A.CallTo(() => rdfTypeProviderPolicy.Create(A<Type>._))
                .ReturnsLazily(call => new Uri("http://example.com/class"));

            this.browser = new Browser(
                configurator =>
                {
                    configurator.Module<TestModule>();
                    configurator.Module<HydraApiDocumentationModule>();
                    configurator.Dependency<IHydraDocumentationSettings>(new TestSettings());
                    configurator.Dependency(rdfTypeProviderPolicy);
                    configurator.Dependency(A.Fake<ISupportedPropertyFactory>());
                    configurator.Dependency(A.Fake<ISupportedClassMetaProvider>());
                    configurator.Dependency(A.Fake<ISupportedOperationFactory>());
                    configurator.ApplicationStartupTask<HydraDocumentationStartup>();
                },
                context => context.HostName("hydra.guru"));
        }

        [Fact]
        public async void Should_append_api_doc_header_to_GET_query()
        {
            // when
            var response = await this.browser.Post("test");

            // then
            response.Headers.Should().ContainKey("Link");
            response.Headers["Link"].Should().Match(ExpectedLinkHeader);
        }

        [Fact]
        public async void Should_append_api_doc_header_to_POST_query()
        {
            // when
            var response = await this.browser.Get("test");

            // then
            response.Headers.Should().ContainKey("Link");
            response.Headers["Link"].Should().Match(ExpectedLinkHeader);
        }

        [Fact]
        public async void Should_not_replace_other_links_with_doc_link()
        {
            // when
            var response = await this.browser.Get("has-link");

            // then
            response.Headers.Should().ContainKey("Link");
            response.Headers["Link"].Should().MatchRegex(ExpectedLinkHeader);
            response.Headers["Link"].Should().MatchRegex("<http://test.com/canonical>; rel=\"canonical\"");
        }

        private class TestModule : NancyModule
        {
            public TestModule()
            {
                this.Get("test", _ => "GET test");
                this.Post("test", _ => "POST test");
                this.Get("has-link", _ => new Response
                {
                    Headers = new Dictionary<string, string>
                    {
                        { "Link", "<http://test.com/canonical>; rel=\"canonical\"" }
                    }
                });
{'fraction_non_alphanumeric': 0.11238532110091744, 'fraction_numerical': 0.0, 'mean_word_length': 2.132375577219087, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 39, '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': '2649232', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1537, 'n_words': 253}