/// <summary> /// Common method to initiate internal fields regardless of which constructor was used. /// </summary> /// <param name="apiKey">Your SendGrid API key.</param> /// <param name="host">Base url (e.g. https://api.sendgrid.com)</param> /// <param name="requestHeaders">A dictionary of request headers</param> /// <param name="version">API version, override AddVersion to customize</param> /// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint)</param> private void InitiateClient(string apiKey, string host, Dictionary <string, string> requestHeaders, string version, string urlPath) { UrlPath = urlPath; Version = version; var baseAddress = host ?? "https://api.sendgrid.com"; var clientVersion = GetType().GetTypeInfo().Assembly.GetName().Version.ToString(); // standard headers client.BaseAddress = new Uri(baseAddress); Dictionary <string, string> headers = new Dictionary <string, string> { { "Authorization", "Bearer " + apiKey }, { "Content-Type", "application/json" }, { "User-Agent", "sendgrid/" + clientVersion + " csharp" }, { "Accept", "application/json" } }; // set header overrides if (requestHeaders != null) { foreach (var header in requestHeaders) { headers[header.Key] = header.Value; } } // add headers to httpClient foreach (var header in headers) { if (header.Key == "Authorization") { var split = header.Value.Split(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(split[0], split[1]); } else if (header.Key == "Content-Type") { client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(header.Value)); MediaType = header.Value; } else { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } // initialize a ContentVerifier contentVerifier = new ContentVerifier(); }
public void VerifyWithoutExpected_ThrowExpectedFileNotFoundAndSaveActual() { const string actualFileName = "actual.txt"; const string expectedFileName = "expected.txt"; const string actualFileContent = "actual file content"; var actualFilePath = Path.Combine(tempDirectory, actualFileName); void VerifyWithoutExpected() => ContentVerifier.UseDirectory(tempDirectory) .SaveActualAs(actualFileName, s => WriteStringToStream(s, actualFileContent)) .ReadExpectedAs(expectedFileName, s => new StreamReader(s).ReadToEnd()) .Verify(expected => Assert.True(false)); Assert.Throws <ExpectedFileNotFoundException>(VerifyWithoutExpected); Assert.True(File.Exists(actualFilePath), $"File.Exists(\"{actualFilePath}\") return false"); Assert.Equal(File.ReadAllText(actualFilePath), actualFileContent); }
public void VerifyWithGoodActual_DoNothing() { const string actualFileName = "actual.txt"; const string expectedFileName = "expected.txt"; const string actualContent = "good content"; const string expectedContent = "good content"; var actualFilePath = Path.Combine(tempDirectory, actualFileName); var expectedFilePath = Path.Combine(tempDirectory, expectedFileName); File.WriteAllText(expectedFilePath, expectedContent); ContentVerifier.UseDirectory(tempDirectory) .SaveActualAs(actualFileName, s => WriteStringToStream(s, actualContent)) .ReadExpectedAs(expectedFileName, s => new StreamReader(s).ReadToEnd()) .Verify(expected => Assert.Equal(actualContent, expected)); Assert.False(File.Exists(actualFilePath), $"File.Exists(\"{actualFilePath}\") return true"); }
public void ActualFileOverwriteOldActual() { const string actualFileName = "actual.txt"; const string expectedFileName = "expected.txt"; const string actualContent = "old looooooooooooooooooooooooooooong actual"; const string expectedContent = "good content"; var actualFilePath = Path.Combine(tempDirectory, actualFileName); var expectedFilePath = Path.Combine(tempDirectory, expectedFileName); File.WriteAllText(expectedFilePath, expectedContent); File.WriteAllText(actualFilePath, actualContent); void Verify() => ContentVerifier.UseDirectory(tempDirectory) .SaveActualAs(actualFileName, s => s.WriteString("new short actual")) .ReadExpectedAs(expectedFileName, s => s.ReadString()) .Verify(expected => Assert.False(true)); Assert.Throws <FalseException>(Verify); Assert.Equal("new short actual", File.ReadAllText(actualFilePath)); }
public void TestApplyRulesToNumbers() { PrepareDirectory(); // arrange using var content = ContentLoader .For <MyContent>() .WithDeserializer(s => s.ReadString().Split(",").Select(int.Parse).ToArray()) // for numbers .WithDeserializer(s => s.ReadString().Split("\n")) // for rules .LoadFromDirectory(testDirectory); // act var actualNumbers = ApplyRulesToNumbers(content.Numbers, content.Rules); // assert ContentVerifier .UseDirectory(testDirectory) .SaveActualAs("actual.num", s => s.WriteString(string.Join(",", actualNumbers))) .ReadExpectedAs("expected.num", s => s.ReadString().Split(",").Select(int.Parse).ToArray()) .Verify(expectedNumbers => Assert.Equal(expectedNumbers, actualNumbers)); }