public static void CommonCounters___Includes_all_declared() { // Arrange & Act var common = PerformanceCounterLibrary.CommonCounters; var type = typeof(PerformanceCounterLibrary); var publics = type.GetPropertiesFiltered(MemberRelationships.DeclaredInType) .Where(_ => _.Name != nameof(PerformanceCounterLibrary.CommonCounters)) .Select(_ => _.GetValue(null)) .ToList(); // Assert var actualCommon = Serializer.SerializeToString(common); var actualPublics = Serializer.SerializeToString(publics); actualPublics.Should().Be(actualCommon); }
/// <inheritdoc /> public override void WriteJson( JsonWriter writer, object value, JsonSerializer serializer) { var payload = value == null ? null : UnderlyingSerializer.SerializeToString(value); var payloadObject = new JValue(payload); payloadObject.WriteTo(writer); }
public static void Model_Recipe_conversions___Roundtrip() { // Arrange var inputDescription = new PerformanceCounterDescription(A.Dummy <string>(), A.Dummy <string>(), A.Dummy <string>(), A.Dummy <float>(), A.Dummy <float>()); var inputSample = new PerformanceCounterSample(inputDescription, A.Dummy <float>()); // Act var actualDescription = inputDescription.ToRecipe().ToModel().FromModel().FromRecipe(); var actualSample = inputSample.ToRecipe().ToModel().FromModel().FromRecipe(); // Assert actualDescription.Should().BeOfType(inputDescription.GetType()); actualSample.Should().BeOfType(inputSample.GetType()); var inputDescriptionJson = Serializer.SerializeToString(inputDescription); var actualDescriptionJson = Serializer.SerializeToString(actualDescription); actualDescriptionJson.Should().Be(inputDescriptionJson); var inputSampleJson = Serializer.SerializeToString(inputSample); var actualSampleJson = Serializer.SerializeToString(actualSample); actualSampleJson.Should().Be(inputSampleJson); }
/// <inheritdoc /> public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, DateTime?value) { new { context }.Must().NotBeNull(); var stringValue = StringSerializer.SerializeToString(value); if (stringValue == null) { context.Writer.WriteNull(); } else { context.Writer.WriteString(stringValue); } }
/// <summary> /// Makes a restful call using supplied information. /// </summary> /// <typeparam name="TResult">Return type to convert response to (if you provide VoidResultType then null will be returned - basically a void call).</typeparam> /// <param name="uri">Uri to make the request against.</param> /// <param name="httpVerb">HTTP verb to use.</param> /// <param name="body">Optional body object to send (use null if not needed).</param> /// <param name="cookieJar">Optional cookie to use (use null if not needed).</param> /// <param name="headerJar">Optional headers to use (use null if not needed).</param> /// <param name="saveResponseHeadersAction">Optional action to use to save response headers (use null if not needed).</param> /// <param name="contentType">Content type to use for request.</param> /// <param name="acceptType">Content type to use for response.</param> /// <param name="timeout">Timeout to use.</param> /// <param name="serializer">Serializer to use.</param> /// <returns>Converted response to the specified type.</returns> /// <exception cref="ArgumentException"> /// Must have return of string when accepting text type. /// or /// ContentType: " + contentType + " not supported at this time. - contentType /// or /// AcceptType: " + contentType + " not supported at this time. - acceptType /// or /// AcceptType: " + acceptType + " not supported at this time. - acceptType. /// </exception> public static TResult Call <TResult>( Uri uri, string httpVerb, object body, CookieJar cookieJar, HeaderJar headerJar, Action <KeyValuePair <string, string>[]> saveResponseHeadersAction, ContentType contentType, ContentType acceptType, TimeSpan timeout, IStringSerializeAndDeserialize serializer) where TResult : class { new { uri }.AsArg().Must().NotBeNull(); new { httpVerb }.AsArg().Must().NotBeNullNorWhiteSpace(); new { serializer }.AsArg().Must().NotBeNull(); if (acceptType == ContentType.TextPlain && typeof(TResult) != typeof(string)) { throw new ArgumentException("Must have return of string when accepting text type."); } if (contentType != ContentType.ApplicationJson) { throw new ArgumentException("ContentType: " + contentType + " not supported at this time.", nameof(contentType)); } if (acceptType != ContentType.ApplicationJson && acceptType != ContentType.TextPlain) { throw new ArgumentException("AcceptType: " + contentType + " not supported at this time.", nameof(acceptType)); } if (timeout == default(TimeSpan)) { timeout = TimeSpan.FromSeconds(100); } var cookieContainer = new CookieContainer(); foreach (var cookie in cookieJar.Cookies) { cookieContainer.Add(cookie); } // ReSharper disable once AccessToStaticMemberViaDerivedType - want to call this method... HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); request.CookieContainer = cookieContainer; request.ContentType = contentType.ToStringValue(); request.Accept = acceptType.ToStringValue(); request.Method = httpVerb; request.Timeout = (int)timeout.TotalMilliseconds; LoadRequestHeaders(request, headerJar); string bodyAsString = null; if (contentType == ContentType.ApplicationJson && body != null) { bodyAsString = serializer.SerializeToString(body); } if (httpVerb != HttpVerb.Get.ToString().ToUpperInvariant() && !string.IsNullOrWhiteSpace(bodyAsString)) { request.ContentLength = bodyAsString.Length; using (var requestStream = request.GetRequestStream()) { using (var requestWriter = new StreamWriter(requestStream, Encoding.ASCII)) { requestWriter.Write(bodyAsString); requestWriter.Close(); } } } string contents = null; WebHeaderCollection responseHeadersRaw; using (var resp = request.GetResponse()) { responseHeadersRaw = resp.Headers; var responseStream = resp.GetResponseStream(); if (responseStream != null) { using (var reader = new StreamReader(responseStream)) { contents = reader.ReadToEnd(); } } } TResult ret = default(TResult); if (typeof(TResult) == typeof(VoidResultType)) { return(ret); // this will just be null and should only be used when you don't want a return } else if (acceptType == ContentType.ApplicationJson) { ret = serializer.Deserialize <TResult>(contents); } else if (acceptType == ContentType.TextPlain) { ret = contents as TResult; } else { throw new ArgumentException("AcceptType: " + acceptType + " not supported at this time.", nameof(acceptType)); } var responseHeaders = responseHeadersRaw == null ? new KeyValuePair <string, string> [0] : responseHeadersRaw.ToKeyValuePairArray(); saveResponseHeadersAction?.Invoke(responseHeaders); return(ret); }
/// <inheritdoc /> public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, DateTime value) { new { context }.Must().NotBeNull(); context.Writer.WriteString(StringSerializer.SerializeToString(value)); }