Пример #1
0
        public async Task CanHandlerxistingAuthorization()
        {
            var accountKey = await Helper.LoadkeyV1();

            var authzUri = new Uri("http://example.com/new-authz");
            var authzLoc = new Uri("http://example.com/authz/111");
            var mock     = MockHttp(async req =>
            {
                if (req.Method == HttpMethod.Post && req.RequestUri == authzUri)
                {
                    var payload = await ParsePayload <AuthorizationEntity>(req);
                    return(CreateResponse(null, HttpStatusCode.SeeOther, authzLoc));
                }

                if (req.Method == HttpMethod.Get && req.RequestUri == authzLoc)
                {
                    return(CreateResponse(new AuthorizationEntity
                    {
                        Identifier = new AuthorizationIdentifier
                        {
                            Type = AuthorizationIdentifierTypes.Dns,
                            Value = "www.example.com",
                        },
                        Status = EntityStatus.Pending,
                    }, HttpStatusCode.OK, authzLoc));
                }

                return(null);
            });

            using (var http = new HttpClient(mock.Object))
                using (var handler = new AcmeHttpHandler(server, http))
                {
                    using (var client = new AcmeClient(handler))
                    {
                        client.Use(accountKey.Export());

                        var authz = await client.NewAuthorization(new AuthorizationIdentifier
                        {
                            Type  = AuthorizationIdentifierTypes.Dns,
                            Value = "www.example.com"
                        });

                        Assert.NotNull(authz);
                        Assert.Equal(authzLoc, authz.Location);
                    }

                    mock.Protected().Verify("Dispose", Times.Never(), true);
                }
        }
Пример #2
0
        public async Task CanCreateRegistration()
        {
            var accountKey = await Helper.LoadkeyV1();

            var regLocation = new Uri("http://example.com/reg/1");
            var mock        = MockHttp(async req =>
            {
                if (req.Method == HttpMethod.Post && req.RequestUri == acmeDir.NewReg)
                {
                    var payload = await ParsePayload <RegistrationEntity>(req);
                    Assert.Equal(ResourceTypes.NewRegistration, payload.Resource);
                    Assert.Equal(1, payload.Contact?.Count);
                    Assert.Equal(email, payload.Contact[0]);

                    var respJson = new
                    {
                        contact  = payload.Contact,
                        resource = ResourceTypes.Registration
                    };

                    var resp = CreateResponse(respJson, HttpStatusCode.Created, regLocation);
                    resp.Headers.Add("Link", $"<{tos}>; rel=\"terms-of-service\"");
                    return(resp);
                }

                return(null);
            });

            using (var http = new HttpClient(mock.Object))
                using (var handler = new AcmeHttpHandler(server, http))
                {
                    using (var client = new AcmeClient(handler))
                    {
                        client.Use(accountKey.Export());

                        var account = await client.NewRegistraton(email);

                        Assert.Equal(ResourceTypes.Registration, account.Data.Resource);
                        Assert.Null(account.Data.Agreement);
                        Assert.Equal(regLocation, account.Location);
                        Assert.Equal(tos, account.GetTermsOfServiceUri());
                    }

                    mock.As <IDisposable>().Verify(x => x.Dispose(), Times.Never());
                }
        }
Пример #3
0
        public async Task CanNewRegistraton()
        {
            var accountKey = await Helper.LoadkeyV1();

            var contacts    = new string[] { "mailto:[email protected]" };
            var regLocation = new Uri("http://example.com/reg/1");
            var mock        = MockHttp(async req =>
            {
                if (req.Method == HttpMethod.Post && req.RequestUri == Helper.MockDirectoryV1.NewReg)
                {
                    var payload = await ParsePayload <RegistrationEntity>(req);
                    Assert.Equal(ResourceTypes.NewRegistration, payload.Resource);
                    Assert.Equal(contacts.Clone(), payload.Contact);

                    var respJson = new
                    {
                        contact   = payload.Contact,
                        agreement = payload.Agreement,
                        resource  = ResourceTypes.Registration
                    };

                    var resp = CreateResponse(respJson, HttpStatusCode.Created, regLocation);
                    resp.Headers.Add("Link", $"<{tos}>; rel=\"terms-of-service\"");
                    return(resp);
                }

                return(null);
            });

            using (var http = new HttpClient(mock.Object))
                using (var handler = new AcmeHttpHandler(server, http))
                {
                    using (var client = new AcmeClient(handler))
                    {
                        client.Use(accountKey.Export());
                        var result = await client.NewRegistraton(contacts);

                        Assert.Equal(ResourceTypes.Registration, result.Data.Resource);
                        Assert.Equal(regLocation, result.Location);
                    }

                    mock.Protected().Verify("Dispose", Times.Never(), true);
                }
        }