rfc8555_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package acme
  5. import (
  6. "context"
  7. "fmt"
  8. "net/http"
  9. "net/http/httptest"
  10. "testing"
  11. )
  12. // While contents of this file is pertinent only to RFC8555,
  13. // it is complementary to the tests in the other _test.go files
  14. // many of which are valid for both pre- and RFC8555.
  15. // This will make it easier to clean up the tests once non-RFC compliant
  16. // code is removed.
  17. func TestRFC_Discover(t *testing.T) {
  18. const (
  19. nonce = "https://example.com/acme/new-nonce"
  20. reg = "https://example.com/acme/new-acct"
  21. order = "https://example.com/acme/new-order"
  22. authz = "https://example.com/acme/new-authz"
  23. revoke = "https://example.com/acme/revoke-cert"
  24. keychange = "https://example.com/acme/key-change"
  25. metaTerms = "https://example.com/acme/terms/2017-5-30"
  26. metaWebsite = "https://www.example.com/"
  27. metaCAA = "example.com"
  28. )
  29. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  30. w.Header().Set("Content-Type", "application/json")
  31. fmt.Fprintf(w, `{
  32. "newNonce": %q,
  33. "newAccount": %q,
  34. "newOrder": %q,
  35. "newAuthz": %q,
  36. "revokeCert": %q,
  37. "keyChange": %q,
  38. "meta": {
  39. "termsOfService": %q,
  40. "website": %q,
  41. "caaIdentities": [%q],
  42. "externalAccountRequired": true
  43. }
  44. }`, nonce, reg, order, authz, revoke, keychange, metaTerms, metaWebsite, metaCAA)
  45. }))
  46. defer ts.Close()
  47. c := Client{DirectoryURL: ts.URL}
  48. dir, err := c.Discover(context.Background())
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. if dir.NonceURL != nonce {
  53. t.Errorf("dir.NonceURL = %q; want %q", dir.NonceURL, nonce)
  54. }
  55. if dir.RegURL != reg {
  56. t.Errorf("dir.RegURL = %q; want %q", dir.RegURL, reg)
  57. }
  58. if dir.OrderURL != order {
  59. t.Errorf("dir.OrderURL = %q; want %q", dir.OrderURL, order)
  60. }
  61. if dir.AuthzURL != authz {
  62. t.Errorf("dir.AuthzURL = %q; want %q", dir.AuthzURL, authz)
  63. }
  64. if dir.RevokeURL != revoke {
  65. t.Errorf("dir.RevokeURL = %q; want %q", dir.RevokeURL, revoke)
  66. }
  67. if dir.KeyChangeURL != keychange {
  68. t.Errorf("dir.KeyChangeURL = %q; want %q", dir.KeyChangeURL, keychange)
  69. }
  70. if dir.Terms != metaTerms {
  71. t.Errorf("dir.Terms = %q; want %q", dir.Terms, metaTerms)
  72. }
  73. if dir.Website != metaWebsite {
  74. t.Errorf("dir.Website = %q; want %q", dir.Website, metaWebsite)
  75. }
  76. if len(dir.CAA) == 0 || dir.CAA[0] != metaCAA {
  77. t.Errorf("dir.CAA = %q; want [%q]", dir.CAA, metaCAA)
  78. }
  79. if !dir.ExternalAccountRequired {
  80. t.Error("dir.Meta.ExternalAccountRequired is false")
  81. }
  82. }