api_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Package api implements a SCEP HTTP server.
  2. package api
  3. import (
  4. "bytes"
  5. "errors"
  6. "net/http"
  7. "net/http/httptest"
  8. "reflect"
  9. "testing"
  10. "testing/iotest"
  11. )
  12. func Test_decodeRequest(t *testing.T) {
  13. type args struct {
  14. r *http.Request
  15. }
  16. tests := []struct {
  17. name string
  18. args args
  19. want request
  20. wantErr bool
  21. }{
  22. {
  23. name: "fail/unsupported-method",
  24. args: args{
  25. r: httptest.NewRequest(http.MethodPatch, "http://scep:8080/?operation=AnUnsupportOperation", nil),
  26. },
  27. want: request{},
  28. wantErr: true,
  29. },
  30. {
  31. name: "fail/get-unsupported-operation",
  32. args: args{
  33. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=AnUnsupportOperation", nil),
  34. },
  35. want: request{},
  36. wantErr: true,
  37. },
  38. {
  39. name: "fail/get-PKIOperation",
  40. args: args{
  41. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=PKIOperation&message='somewronginput'", nil),
  42. },
  43. want: request{},
  44. wantErr: true,
  45. },
  46. {
  47. name: "fail/post-PKIOperation",
  48. args: args{
  49. r: httptest.NewRequest(http.MethodPost, "http://scep:8080/?operation=PKIOperation", iotest.ErrReader(errors.New("a read error"))),
  50. },
  51. want: request{},
  52. wantErr: true,
  53. },
  54. {
  55. name: "ok/get-GetCACert",
  56. args: args{
  57. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=GetCACert", nil),
  58. },
  59. want: request{
  60. Operation: "GetCACert",
  61. Message: []byte{},
  62. },
  63. wantErr: false,
  64. },
  65. {
  66. name: "ok/get-GetCACaps",
  67. args: args{
  68. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=GetCACaps", nil),
  69. },
  70. want: request{
  71. Operation: "GetCACaps",
  72. Message: []byte{},
  73. },
  74. wantErr: false,
  75. },
  76. {
  77. name: "ok/get-PKIOperation",
  78. args: args{
  79. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=PKIOperation&message=MTIzNA==", nil),
  80. },
  81. want: request{
  82. Operation: "PKIOperation",
  83. Message: []byte("1234"),
  84. },
  85. wantErr: false,
  86. },
  87. {
  88. name: "ok/post-PKIOperation",
  89. args: args{
  90. r: httptest.NewRequest(http.MethodPost, "http://scep:8080/?operation=PKIOperation", bytes.NewBufferString("1234")),
  91. },
  92. want: request{
  93. Operation: "PKIOperation",
  94. Message: []byte("1234"),
  95. },
  96. wantErr: false,
  97. },
  98. }
  99. for _, tt := range tests {
  100. t.Run(tt.name, func(t *testing.T) {
  101. got, err := decodeRequest(tt.args.r)
  102. if (err != nil) != tt.wantErr {
  103. t.Errorf("decodeRequest() error = %v, wantErr %v", err, tt.wantErr)
  104. return
  105. }
  106. if !reflect.DeepEqual(got, tt.want) {
  107. t.Errorf("decodeRequest() = %v, want %v", got, tt.want)
  108. }
  109. })
  110. }
  111. }