|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.IO; |
| 6 | +using System.Net; |
| 7 | +using System.Net.Http; |
| 8 | +using System.Net.Http.Headers; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using System.Web.Http; |
| 11 | +using WebJobs.Script.WebHost; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace WebJobs.Script.Tests |
| 15 | +{ |
| 16 | + public class SamplesEndToEndTests : IClassFixture<SamplesEndToEndTests.TestFixture> |
| 17 | + { |
| 18 | + private TestFixture _fixture; |
| 19 | + |
| 20 | + public SamplesEndToEndTests(TestFixture fixture) |
| 21 | + { |
| 22 | + _fixture = fixture; |
| 23 | + } |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public async Task Home_Get_Succeeds() |
| 27 | + { |
| 28 | + HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, string.Empty); |
| 29 | + |
| 30 | + HttpResponseMessage response = await this._fixture.Client.SendAsync(request); |
| 31 | + Assert.Equal(HttpStatusCode.NoContent, response.StatusCode); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public async Task HttpTrigger_Get_Succeeds() |
| 36 | + { |
| 37 | + string uri = "api/httptrigger?code=hyexydhln844f2mb7hgsup2yf8dowlb0885mbiq1&name=Mathew"; |
| 38 | + HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri); |
| 39 | + |
| 40 | + HttpResponseMessage response = await this._fixture.Client.SendAsync(request); |
| 41 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 42 | + string body = await response.Content.ReadAsStringAsync(); |
| 43 | + Assert.Equal("Hello Mathew", body); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public async Task GenericWebHook_Post_Succeeds() |
| 48 | + { |
| 49 | + string uri = "api/webhook-generic?code=1388a6b0d05eca2237f10e4a4641260b0a08f3a5"; |
| 50 | + HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri); |
| 51 | + request.Content = new StringContent("{ 'a': 'Foobar' }"); |
| 52 | + request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); |
| 53 | + |
| 54 | + HttpResponseMessage response = await this._fixture.Client.SendAsync(request); |
| 55 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 56 | + string body = await response.Content.ReadAsStringAsync(); |
| 57 | + Assert.Equal("WebHook processed successfully! Foobar", body); |
| 58 | + } |
| 59 | + |
| 60 | + [Fact] |
| 61 | + public async Task GenericWebHook_Post_AdminKey_Succeeds() |
| 62 | + { |
| 63 | + // Verify that sending the admin key bypasses WebHook auth |
| 64 | + string uri = "api/webhook-generic?code=t8laajal0a1ajkgzoqlfv5gxr4ebhqozebw4qzdy"; |
| 65 | + HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri); |
| 66 | + request.Content = new StringContent("{ 'a': 'Foobar' }"); |
| 67 | + request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); |
| 68 | + |
| 69 | + HttpResponseMessage response = await this._fixture.Client.SendAsync(request); |
| 70 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 71 | + string body = await response.Content.ReadAsStringAsync(); |
| 72 | + Assert.Equal("WebHook processed successfully! Foobar", body); |
| 73 | + } |
| 74 | + |
| 75 | + public class TestFixture |
| 76 | + { |
| 77 | + public TestFixture() |
| 78 | + { |
| 79 | + HttpConfiguration config = new HttpConfiguration(); |
| 80 | + |
| 81 | + WebHostSettings settings = new WebHostSettings |
| 82 | + { |
| 83 | + IsSelfHost = true, |
| 84 | + ScriptPath = Path.Combine(Environment.CurrentDirectory, @"..\..\..\..\sample"), |
| 85 | + LogPath = Path.Combine(Path.GetTempPath(), @"Functions"), |
| 86 | + SecretsPath = Path.Combine(Environment.CurrentDirectory, @"..\..\..\..\src\WebJobs.Script.WebHost\App_Data\Secrets") |
| 87 | + }; |
| 88 | + WebApiConfig.Register(config, settings); |
| 89 | + |
| 90 | + HttpServer server = new HttpServer(config); |
| 91 | + this.Client = new HttpClient(server); |
| 92 | + this.Client.BaseAddress = new Uri("https://localhost/"); |
| 93 | + } |
| 94 | + |
| 95 | + public HttpClient Client { get; set; } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments