private void CreateCommandTest(Func <ApiCommandFactory, IApiCommandAsync> create, Type commandType, int serializerMapCalls) { // Arrange IApiHttpFactory httpFactory = Substitute.For <IApiHttpFactory>(); IHttpResponseFactory responseFactory = Substitute.For <IHttpResponseFactory>(); IApiHttpClient httpClient = Substitute.For <IApiHttpClient>(); IApiCommandContentSerializer serializer = Substitute.For <IApiCommandContentSerializer>(); IApiCommandContentTypeMap <IApiCommandContentSerializer> serializers = Substitute.For <IApiCommandContentTypeMap <IApiCommandContentSerializer> >(); serializers[_mediaType].Returns(serializer); httpFactory.NewClient(_key).Returns(httpClient); var factory = new ApiCommandFactory(httpFactory, responseFactory, serializers); // Act var command = create(factory); // Assert Assert.NotNull(command); Assert.IsType(commandType, command); httpFactory.Received(1).NewClient(Arg.Any <object>()); httpFactory.Received(1).NewClient(_key); _ = serializers.Received(serializerMapCalls)[_mediaType]; }
public void CreateRequest_Execute_RestursRequest() { // Arrange Uri uri = new("/", UriKind.Relative); object content = new(); IApiHttpFactory httpFactory = Substitute.For <IApiHttpFactory>(); IHttpResponseFactory responseFactory = Substitute.For <IHttpResponseFactory>(); IApiCommandContentSerializer serializer = Substitute.For <IApiCommandContentSerializer>(); IApiCommandContentTypeMap <IApiCommandContentSerializer> serializers = Substitute.For <IApiCommandContentTypeMap <IApiCommandContentSerializer> >(); var factory = new ApiCommandFactory(httpFactory, responseFactory, serializers); // Act var request = factory.CreateRequest(uri, content); // Assert Assert.NotNull(request); Assert.Equal(uri, request.ApiUri); Assert.Same(content, request.Content); }
/// <summary> /// Create a new instance of the command factory. /// </summary> /// <param name="httpFactory">An instance of the api http factory used to get <see cref="IApiHttpClient"/> instances.</param> /// <param name="responseFactory">An instance of the response factory to interpret the responses from the server.</param> /// <param name="serializers">A map of serializers that is used deserialize the serialize the content that is sent to the server.</param> public ApiCommandFactory(IApiHttpFactory httpFactory, IHttpResponseFactory responseFactory, IApiCommandContentTypeMap <IApiCommandContentSerializer> serializers) { _httpFactory = httpFactory ?? throw new ArgumentNullException(nameof(httpFactory)); _responseFactory = responseFactory ?? throw new ArgumentNullException(nameof(responseFactory)); _serializers = serializers ?? throw new ArgumentNullException(nameof(serializers)); }
/// <summary> /// Configure the JWT pipeline, first the refresh token handler -> login handler -> auth handler. Note, these handlers will be ignored if Auth header has already been filled in. /// 1. The token handler will try and refresh the token if the access token is expired /// 2. The login handler will call the <see cref="ILoginUserPasswordAsync"/> to request the the username and password and will request the server for a new access token. /// 3. Will add the access token to the appropriate header value. /// </summary> /// <param name="factory"></param> public static void ConfigureUserNamePasswordJwt(this IApiHttpFactory factory) { factory.AddRequestHandler <HttpSecurityTokenRefreshHandler>(SecurityTokenConstants.ApiClientName) .SetNextHandler <HttpSecurityTokenLoginHandler>() .SetNextHandler <HttpSecurityAuthHeaderHandler>(); }