/// <summary> /// Generate Authentication header for HTTP request /// </summary> /// <param name="request"></param> /// <param name="bombardierOptions"></param> /// <returns></returns> internal static string GenerateAuthHeader(HttpRequest request, BombardierGeneratorOptions bombardierOptions) { //Check if Swagger operation description contains certain auth tags string authHeader; if (request.Description.Contains(AuthenticationType.OAuth2.Value()) || bombardierOptions.AccessTokens.Any()) { if (request.Description.Contains(AuthenticationType.Customer.Value()) && !request.Description.Contains(AuthenticationType.Administrator.Value())) { authHeader = GetOauth2AuthenticationHeader(bombardierOptions.AccessTokens, AuthenticationType.Customer); } else if (!request.Description.Contains(AuthenticationType.Customer.Value()) && request.Description.Contains(AuthenticationType.Administrator.Value())) { authHeader = GetOauth2AuthenticationHeader(bombardierOptions.AccessTokens, AuthenticationType.Administrator); } else { authHeader = GetOauth2AuthenticationHeader(bombardierOptions.AccessTokens, AuthenticationType.Customer); } } else if (request.Description.Contains(AuthenticationType.ApiKey.Value()) || bombardierOptions.ApiKey != null) { authHeader = GetApiKeyAuthenticationHeader(bombardierOptions); } else if (request.Description.Contains(AuthenticationType.Basic.Value()) || (bombardierOptions.UserName != null && bombardierOptions.Password != null)) { authHeader = GetBasicAuthenticationHeader(bombardierOptions); } else { authHeader = String.Empty; } return(authHeader); }
public void BombardierGeneratorOptionsAddApiKeyTest_Successful() { var options = new BombardierGeneratorOptions(); options.AddApiKey("12345"); Assert.Equal("12345", options.ApiKey); }
/// <summary> /// Generate Insecure Bombardier switch /// </summary> /// <param name="bombardierOptions"></param> /// <returns></returns> internal static string GenerateInsecureSwitch(BombardierGeneratorOptions bombardierOptions) { if (bombardierOptions.BombardierInsecure) { return(" --insecure"); } return(String.Empty); }
public void BombardierGeneratorOptionsAddOAuth2TokenTest_Successful() { var options = new BombardierGeneratorOptions(); options.AddOAuth2Token("token", AuthenticationType.Administrator); Assert.Equal(AuthenticationType.Administrator, options.AccessTokens.FirstOrDefault(t => t.Key == AuthenticationType.Administrator).Key); Assert.Equal("token", options.AccessTokens.FirstOrDefault(t => t.Key == AuthenticationType.Administrator).Value); }
public void BombardierGeneratorOptionsAddBasicAuthenticationTest_Successful() { var options = new BombardierGeneratorOptions(); options.AddBasicAuthentication("user", "password"); Assert.Equal("user", options.UserName); Assert.Equal("password", options.Password); }
/// <summary> /// Generate bombardier payload body switch /// </summary> /// <param name="request"></param> /// <param name="bombardierGeneratorOptions"></param> /// <returns></returns> internal static object GenerateBodySwitch(HttpRequest request, BombardierGeneratorOptions bombardierGeneratorOptions) { var body = HttpBodyHelper.GenerateHttpRequestBody(request, bombardierGeneratorOptions.BombardierBodyContentType, bombardierGeneratorOptions.ReplacementValues); if (string.IsNullOrEmpty(body)) { return(String.Empty); } return($" -b \"{body.Replace(@"""", @"\""")}\""); }
/// <summary> /// Generate and replace URL parameters with replacement values /// </summary> /// <param name="request"></param> /// <param name="bombardierGeneratorOptions"></param> /// <returns></returns> internal static string GenerateUrlParameters(HttpRequest request, BombardierGeneratorOptions bombardierGeneratorOptions) { var generator = new HttpRequestUrlGenerator(request, options => { options.AddReplacementValues(bombardierGeneratorOptions.ReplacementValues); }); var url = generator.GetUrl(); return(url); }
/// <summary> /// Generate Rate limit for request. /// </summary> /// <returns></returns> internal static string GenerateRateLimitSwitch(BombardierGeneratorOptions bombardierOptions) { if (bombardierOptions.BombardierRateLimit != null && bombardierOptions.BombardierNumberOfTotalRequests != null) { throw new QAToolKitBombardierException("BombardierRateLimit and BombardierNumberOfTotalRequests settings are mutually exclusive. Please use one or the other."); } if (bombardierOptions.BombardierRateLimit != null) { return($" --rate={bombardierOptions.BombardierRateLimit}"); } return(String.Empty); }
private static string GetApiKeyAuthenticationHeader(BombardierGeneratorOptions bombardierOptions) { string authHeader; if (!string.IsNullOrEmpty(bombardierOptions.ApiKey)) { authHeader = $" -H \"ApiKey: {bombardierOptions.ApiKey}\""; } else { throw new QAToolKitBombardierException($"Api Key is missing and is required."); } return(authHeader); }
private static string GetBasicAuthenticationHeader(BombardierGeneratorOptions bombardierOptions) { string authHeader; if (!string.IsNullOrEmpty(bombardierOptions.UserName) && !string.IsNullOrEmpty(bombardierOptions.Password)) { string credentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(bombardierOptions.UserName + ":" + bombardierOptions.Password)); authHeader = $" -H \"Authorization: Basic {credentials}\""; } else { throw new QAToolKitBombardierException($"User name and password for basic authentication are missing and are required)."); } return(authHeader); }
public void BombardierGeneratorOptionsTest_Successful() { var options = new BombardierGeneratorOptions { BombardierConcurrentUsers = 1, BombardierDuration = 12, BombardierRateLimit = 3, BombardierTimeout = 30, BombardierUseHttp2 = true }; Assert.Equal(1, options.BombardierConcurrentUsers); Assert.Equal(12, options.BombardierDuration); Assert.Equal(3, options.BombardierRateLimit); Assert.Equal(30, options.BombardierTimeout); Assert.True(options.BombardierUseHttp2); Assert.False(options.BombardierInsecure); }
/// <summary> /// Generate HTTP protocol switch /// </summary> /// <param name="bombardierGeneratorOptions"></param> /// <returns></returns> internal static object GenerateHttpProtocolSwitch(BombardierGeneratorOptions bombardierGeneratorOptions) { return($" --{(Convert.ToBoolean(bombardierGeneratorOptions.BombardierUseHttp2) ? "http2" : "http1")}"); }
/// <summary> /// Generate timeout Bombardier switch /// </summary> /// <param name="bombardierGeneratorOptions"></param> /// <returns></returns> internal static object GenerateTimeoutSwitch(BombardierGeneratorOptions bombardierGeneratorOptions) { return($" --timeout={bombardierGeneratorOptions.BombardierTimeout}s"); }
/// <summary> /// Generate a Bombardier duration switch /// </summary> /// <param name="bombardierGeneratorOptions"></param> /// <returns></returns> internal static string GenerateDurationSwitch(BombardierGeneratorOptions bombardierGeneratorOptions) { return($" --duration={bombardierGeneratorOptions.BombardierDuration}s"); }
/// <summary> /// Generate concurrent users bombardier switch /// </summary> /// <param name="bombardierGeneratorOptions"></param> /// <returns></returns> internal static object GenerateConcurrentSwitch(BombardierGeneratorOptions bombardierGeneratorOptions) { return($" -c {bombardierGeneratorOptions.BombardierConcurrentUsers}"); }