public async Task NullWillCreateEmptyContent() { var encodedContent = new CustomFormUrlEncodedContent(null); var result = await encodedContent.ReadAsStringAsync(); Assert.Equal(string.Empty, result); }
private HttpContent CreateHttpContent(RestRequest restRequest) { if (restRequest.Method != HttpMethod.Post && restRequest.Method != HttpMethod.Put) { return(null); } HttpContent httpContent; switch (restRequest.ContentType) { case RestContentType.Json: var json = this.Serializer.Serialize(restRequest.Content); httpContent = new StringContent(json, restRequest.ContentEncoding, "application/json"); if (restRequest.ContentEncoding == null) { httpContent.Headers.ContentType.CharSet = null; } httpContent.Headers.ContentLength = json.Length; break; case RestContentType.FormUrlEncoded: httpContent = new CustomFormUrlEncodedContent(restRequest.Content); break; default: httpContent = new NullContent(); break; } if (httpContent.Headers.ContentLength == null) { httpContent.Headers.ContentLength = 0; } return(httpContent); }
public async Task PropertiesWillBeAddedAsExpected() { var encodedContent = new CustomFormUrlEncodedContent(new { foo = "bar", Bar = "föö" }); var result = await encodedContent.ReadAsStringAsync(); Assert.Equal("foo=bar&bar=f%C3%B6%C3%B6", result); }
public async Task KeyValueCollectionWillBeAddedAsExpected() { var encodedContent = new CustomFormUrlEncodedContent(new KeyValuePair <string, string>[] { new KeyValuePair <string, string>("foo", "bar"), new KeyValuePair <string, string>("bar", "föö") }); var result = await encodedContent.ReadAsStringAsync(); Assert.Equal("foo=bar&bar=f%C3%B6%C3%B6", result); }
/// <summary> /// "Compiles" a JS-code /// </summary> /// <param name="content">Text content written on JavaScript</param> /// <param name="path">Path to JS-file</param> /// <param name="externsDependencies">List of JS-externs dependencies</param> /// <param name="options">Compilation options</param> /// <returns>Compiled JS-code</returns> public string Compile(string content, string path, DependencyCollection externsDependencies, RemoteJsCompilationOptions options = null) { string newContent; DependencyCollection allExternsDependencies = new DependencyCollection(); RemoteJsCompilationOptions currentOptions; IList<FormItem> currentOptionsFormItems; if (options != null) { currentOptions = options; currentOptionsFormItems = ConvertCompilationOptionsToFormItems(currentOptions); } else { currentOptions = _defaultOptions; currentOptionsFormItems = _defaultOptionsFormItems; } var formItems = new List<FormItem>(); formItems.Add(new FormItem("js_code", content)); if (currentOptions.CompilationLevel == CompilationLevel.Advanced && (_commonExternsDependencies.Count > 0 || externsDependencies.Count > 0)) { allExternsDependencies.AddRange(_commonExternsDependencies); foreach (Dependency externsDependency in externsDependencies) { if (!_commonExternsDependencies.ContainsUrl(externsDependency.Url)) { allExternsDependencies.Add(externsDependency); } } foreach (Dependency externsDependency in allExternsDependencies) { formItems.Add(new FormItem("js_externs", externsDependency.Content)); } } formItems.AddRange(currentOptionsFormItems); HttpContent httpContent = new CustomFormUrlEncodedContent(formItems); using (var client = new HttpClient()) { HttpResponseMessage response; try { response = client .PostAsync(new Uri(_closureCompilerServiceUrl), httpContent) .Result ; } catch (AggregateException e) { Exception innerException = e.InnerException; if (innerException != null) { if (innerException is HttpRequestException) { throw new Exception( string.Format(Strings.Minifiers_ClosureRemoteMinificationHttpRequestError, _closureCompilerServiceUrl), innerException); } throw innerException; } throw; } if (response.IsSuccessStatusCode) { var result = response.Content.ReadAsStringAsync().Result; var json = JObject.Parse(result); var serverErrors = json["serverErrors"] != null ? json["serverErrors"] as JArray : null; if (serverErrors != null && serverErrors.Count > 0) { throw new ClosureCompilingException( FormatErrorDetails(serverErrors[0], ErrorType.ServerError, path, allExternsDependencies) ); } var errors = json["errors"] != null ? json["errors"] as JArray : null; if (errors != null && errors.Count > 0) { throw new ClosureCompilingException( FormatErrorDetails(errors[0], ErrorType.Error, path, allExternsDependencies) ); } if (currentOptions.Severity > 0) { var warnings = json["warnings"] != null ? json["warnings"] as JArray : null; if (warnings != null && warnings.Count > 0) { throw new ClosureCompilingException( FormatErrorDetails(warnings[0], ErrorType.Warning, path, allExternsDependencies) ); } } newContent = json.Value<string>("compiledCode"); } else { throw new AssetMinificationException( string.Format(Strings.Minifiers_ClosureRemoteMinificationInvalidHttpStatus, response.StatusCode)); } } return newContent; }
/// <summary> /// Sends request using http /// </summary> /// <param name="redcapApi"></param> /// <param name="payload">data</param> /// <param name="uri">URI of the api instance</param> /// <returns></returns> public static async Task <string> SendPostRequestAsync(this RedcapApi redcapApi, Dictionary <string, string> payload, Uri uri) { try { string _responseMessage = Empty; using (var handler = GetHttpHandler()) using (var client = new HttpClient(handler)) { // extract the filepath var pathValue = payload.Where(x => x.Key == "filePath").FirstOrDefault().Value; var pathkey = payload.Where(x => x.Key == "filePath").FirstOrDefault().Key; if (!string.IsNullOrEmpty(pathkey)) { // the actual payload does not contain a 'filePath' key payload.Remove(pathkey); } using (var content = new CustomFormUrlEncodedContent(payload)) { using (var response = await client.PostAsync(uri, content)) { if (response.IsSuccessStatusCode) { // Get the filename so we can save with the name var headers = response.Content.Headers; var fileName = headers.ContentType?.Parameters.Select(x => x.Value).FirstOrDefault(); if (!string.IsNullOrEmpty(fileName)) { var contentDisposition = response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = fileName }; } if (!string.IsNullOrEmpty(pathValue)) { var fileExtension = payload.Where(x => x.Key == "content" && x.Value == "pdf").SingleOrDefault().Value; if (!string.IsNullOrEmpty(fileExtension)) { // pdf fileName = payload.Where(x => x.Key == "instrument").SingleOrDefault().Value; // to do , make extensions for various types // save the file to a specified location using an extension method await response.Content.ReadAsFileAsync(fileName, pathValue, true, fileExtension); } else { await response.Content.ReadAsFileAsync(fileName, pathValue, true, fileExtension); } _responseMessage = fileName; } else { _responseMessage = await response.Content.ReadAsStringAsync(); } } else { _responseMessage = await response.Content.ReadAsStringAsync(); } } } return(_responseMessage); } } catch (Exception Ex) { Log.Error($"{Ex.Message}"); return(Empty); } }