-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenapi_test.go
More file actions
696 lines (625 loc) · 16.8 KB
/
openapi_test.go
File metadata and controls
696 lines (625 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
package openapi
import (
"testing"
)
func TestOpenAPI_Structure(t *testing.T) {
spec := &OpenAPI{
OpenAPI: "3.1.0",
Info: Info{
Title: "Test API",
Version: "1.0.0",
Description: "A test API",
},
Paths: make(map[string]PathItem),
Components: &Components{
Schemas: make(map[string]*Schema),
},
}
if spec.OpenAPI != "3.1.0" {
t.Errorf("expected openapi '3.1.0', got %q", spec.OpenAPI)
}
if spec.Info.Title != "Test API" {
t.Errorf("expected title 'Test API', got %q", spec.Info.Title)
}
if spec.Paths == nil {
t.Error("paths should not be nil")
}
if spec.Components == nil {
t.Error("components should not be nil")
}
}
func TestInfo(t *testing.T) {
info := Info{
Title: "My API",
Version: "2.0.0",
Description: "API Description",
Contact: &Contact{
Name: "Support",
URL: "https://example.com",
Email: "support@example.com",
},
}
if info.Title != "My API" {
t.Errorf("expected title 'My API', got %q", info.Title)
}
if info.Version != "2.0.0" {
t.Errorf("expected version '2.0.0', got %q", info.Version)
}
if info.Contact == nil {
t.Fatal("contact should not be nil")
}
if info.Contact.Email != "support@example.com" {
t.Errorf("expected email 'support@example.com', got %q", info.Contact.Email)
}
}
func TestServer(t *testing.T) {
server := Server{
URL: "https://api.example.com",
Description: "Production server",
}
if server.URL != "https://api.example.com" {
t.Errorf("expected url 'https://api.example.com', got %q", server.URL)
}
if server.Description != "Production server" {
t.Errorf("expected description 'Production server', got %q", server.Description)
}
}
func TestPathItem(t *testing.T) {
pathItem := PathItem{
Summary: "User operations",
Description: "Operations for user management",
Get: &Operation{
OperationID: "getUser",
Summary: "Get user",
},
Post: &Operation{
OperationID: "createUser",
Summary: "Create user",
},
}
if pathItem.Summary != "User operations" {
t.Errorf("expected summary 'User operations', got %q", pathItem.Summary)
}
if pathItem.Get == nil {
t.Fatal("GET operation should not be nil")
}
if pathItem.Get.OperationID != "getUser" {
t.Errorf("expected operation ID 'getUser', got %q", pathItem.Get.OperationID)
}
}
func TestOperation(t *testing.T) {
operation := Operation{
Tags: []string{"users"},
Summary: "Get user by ID",
Description: "Returns a single user",
OperationID: "getUserById",
Parameters: []Parameter{
{
Name: "id",
In: "path",
Required: true,
Schema: &Schema{Type: NewSchemaType("string")},
},
},
Responses: map[string]Response{
"200": {
Description: "Success",
},
},
}
if len(operation.Tags) != 1 || operation.Tags[0] != "users" {
t.Errorf("expected tags ['users'], got %v", operation.Tags)
}
if operation.OperationID != "getUserById" {
t.Errorf("expected operation ID 'getUserById', got %q", operation.OperationID)
}
if len(operation.Parameters) != 1 {
t.Errorf("expected 1 parameter, got %d", len(operation.Parameters))
}
if len(operation.Responses) != 1 {
t.Errorf("expected 1 response, got %d", len(operation.Responses))
}
}
func TestParameter(t *testing.T) {
param := Parameter{
Name: "userId",
In: "path",
Description: "User ID",
Required: true,
Schema: &Schema{Type: NewSchemaType("string")},
}
if param.Name != "userId" {
t.Errorf("expected name 'userId', got %q", param.Name)
}
if param.In != "path" {
t.Errorf("expected in 'path', got %q", param.In)
}
if !param.Required {
t.Error("expected required to be true")
}
if param.Schema.Type.String() != "string" {
t.Errorf("expected schema type 'string', got %q", param.Schema.Type.String())
}
}
func TestRequestBody(t *testing.T) {
reqBody := RequestBody{
Description: "User object",
Required: true,
Content: map[string]MediaType{
"application/json": {
Schema: &Schema{Ref: "#/components/schemas/User"},
},
},
}
if reqBody.Description != "User object" {
t.Errorf("expected description 'User object', got %q", reqBody.Description)
}
if !reqBody.Required {
t.Error("expected required to be true")
}
if len(reqBody.Content) != 1 {
t.Errorf("expected 1 content type, got %d", len(reqBody.Content))
}
}
func TestResponse(t *testing.T) {
response := Response{
Description: "Successful operation",
Content: map[string]MediaType{
"application/json": {
Schema: &Schema{Type: NewSchemaType("object")},
},
},
}
if response.Description != "Successful operation" {
t.Errorf("expected description 'Successful operation', got %q", response.Description)
}
if len(response.Content) != 1 {
t.Errorf("expected 1 content type, got %d", len(response.Content))
}
}
func TestSchema_BasicTypes(t *testing.T) {
tests := []struct {
name string
schema Schema
}{
{"string", Schema{Type: NewSchemaType("string")}},
{"integer", Schema{Type: NewSchemaType("integer")}},
{"number", Schema{Type: NewSchemaType("number")}},
{"boolean", Schema{Type: NewSchemaType("boolean")}},
{"array", Schema{Type: NewSchemaType("array"), Items: &Schema{Type: NewSchemaType("string")}}},
{"object", Schema{Type: NewSchemaType("object"), Properties: map[string]*Schema{
"name": {Type: NewSchemaType("string")},
}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.schema.Type.String() != tt.name {
t.Errorf("expected type %q, got %q", tt.name, tt.schema.Type.String())
}
})
}
}
func TestSchema_WithRef(t *testing.T) {
schema := Schema{
Ref: "#/components/schemas/User",
}
if schema.Ref != "#/components/schemas/User" {
t.Errorf("expected ref '#/components/schemas/User', got %q", schema.Ref)
}
}
func TestSchema_WithRequired(t *testing.T) {
schema := Schema{
Type: NewSchemaType("object"),
Properties: map[string]*Schema{
"name": {Type: NewSchemaType("string")},
"email": {Type: NewSchemaType("string")},
},
Required: []string{"name", "email"},
}
if len(schema.Required) != 2 {
t.Errorf("expected 2 required fields, got %d", len(schema.Required))
}
}
func TestSchema_WithFormat(t *testing.T) {
schema := Schema{
Type: NewSchemaType("string"),
Format: "date-time",
}
if schema.Format != "date-time" {
t.Errorf("expected format 'date-time', got %q", schema.Format)
}
}
func TestComponents(t *testing.T) {
components := Components{
Schemas: map[string]*Schema{
"User": {
Type: NewSchemaType("object"),
Properties: map[string]*Schema{
"id": {Type: NewSchemaType("integer")},
"name": {Type: NewSchemaType("string")},
},
},
},
Responses: map[string]*Response{
"NotFound": {
Description: "Resource not found",
},
},
}
if len(components.Schemas) != 1 {
t.Errorf("expected 1 schema, got %d", len(components.Schemas))
}
if len(components.Responses) != 1 {
t.Errorf("expected 1 response, got %d", len(components.Responses))
}
}
func TestTag(t *testing.T) {
tag := Tag{
Name: "users",
Description: "User management operations",
}
if tag.Name != "users" {
t.Errorf("expected name 'users', got %q", tag.Name)
}
if tag.Description != "User management operations" {
t.Errorf("expected description 'User management operations', got %q", tag.Description)
}
}
func TestTagGroup(t *testing.T) {
group := TagGroup{
Name: "Account",
Tags: []string{"Authentication", "Users"},
}
if group.Name != "Account" {
t.Errorf("expected name 'Account', got %q", group.Name)
}
if len(group.Tags) != 2 {
t.Errorf("expected 2 tags, got %d", len(group.Tags))
}
if group.Tags[0] != "Authentication" {
t.Errorf("expected first tag 'Authentication', got %q", group.Tags[0])
}
}
func TestMediaType(t *testing.T) {
mediaType := MediaType{
Schema: &Schema{Type: NewSchemaType("object")},
Example: map[string]any{
"name": "John",
"age": 30,
},
}
if mediaType.Schema.Type.String() != "object" {
t.Errorf("expected schema type 'object', got %q", mediaType.Schema.Type.String())
}
if mediaType.Example == nil {
t.Error("expected example to be set")
}
}
func TestSecurityScheme_APIKey(t *testing.T) {
scheme := SecurityScheme{
Type: "apiKey",
Name: "api_key",
In: "header",
Description: "API key authentication",
}
if scheme.Type != "apiKey" {
t.Errorf("expected type 'apiKey', got %q", scheme.Type)
}
if scheme.Name != "api_key" {
t.Errorf("expected name 'api_key', got %q", scheme.Name)
}
if scheme.In != "header" {
t.Errorf("expected in 'header', got %q", scheme.In)
}
}
func TestSecurityScheme_OAuth2(t *testing.T) {
scheme := SecurityScheme{
Type: "oauth2",
Flows: &OAuthFlows{
AuthorizationCode: &OAuthFlow{
AuthorizationURL: "https://example.com/oauth/authorize",
TokenURL: "https://example.com/oauth/token",
Scopes: map[string]string{
"read:users": "Read user data",
"write:users": "Write user data",
},
},
},
}
if scheme.Type != "oauth2" {
t.Errorf("expected type 'oauth2', got %q", scheme.Type)
}
if scheme.Flows == nil {
t.Fatal("flows should not be nil")
}
if scheme.Flows.AuthorizationCode == nil {
t.Fatal("authorization code flow should not be nil")
}
if len(scheme.Flows.AuthorizationCode.Scopes) != 2 {
t.Errorf("expected 2 scopes, got %d", len(scheme.Flows.AuthorizationCode.Scopes))
}
}
func TestSecurityScheme_HTTP(t *testing.T) {
scheme := SecurityScheme{
Type: "http",
Scheme: "bearer",
BearerFormat: "JWT",
}
if scheme.Type != "http" {
t.Errorf("expected type 'http', got %q", scheme.Type)
}
if scheme.Scheme != "bearer" {
t.Errorf("expected scheme 'bearer', got %q", scheme.Scheme)
}
if scheme.BearerFormat != "JWT" {
t.Errorf("expected bearer format 'JWT', got %q", scheme.BearerFormat)
}
}
func TestSecurityRequirement(t *testing.T) {
req := SecurityRequirement{
"oauth2": {"read:users", "write:users"},
"apiKey": {},
}
if len(req) != 2 {
t.Errorf("expected 2 security requirements, got %d", len(req))
}
if len(req["oauth2"]) != 2 {
t.Errorf("expected 2 oauth2 scopes, got %d", len(req["oauth2"]))
}
}
func TestExternalDocumentation(t *testing.T) {
docs := ExternalDocumentation{
Description: "Find more info here",
URL: "https://example.com/docs",
}
if docs.URL != "https://example.com/docs" {
t.Errorf("expected url 'https://example.com/docs', got %q", docs.URL)
}
if docs.Description != "Find more info here" {
t.Errorf("expected description 'Find more info here', got %q", docs.Description)
}
}
func TestSchema_Composition(t *testing.T) {
schema := Schema{
AllOf: []*Schema{
{Ref: "#/components/schemas/Base"},
{
Type: NewSchemaType("object"),
Properties: map[string]*Schema{
"extra": {Type: NewSchemaType("string")},
},
},
},
}
if len(schema.AllOf) != 2 {
t.Errorf("expected 2 allOf schemas, got %d", len(schema.AllOf))
}
}
func TestSchema_OneOf(t *testing.T) {
schema := Schema{
OneOf: []*Schema{
{Ref: "#/components/schemas/Cat"},
{Ref: "#/components/schemas/Dog"},
},
Discriminator: &Discriminator{
PropertyName: "petType",
Mapping: map[string]string{
"cat": "#/components/schemas/Cat",
"dog": "#/components/schemas/Dog",
},
},
}
if len(schema.OneOf) != 2 {
t.Errorf("expected 2 oneOf schemas, got %d", len(schema.OneOf))
}
if schema.Discriminator == nil {
t.Fatal("discriminator should not be nil")
}
if schema.Discriminator.PropertyName != "petType" {
t.Errorf("expected property name 'petType', got %q", schema.Discriminator.PropertyName)
}
}
func TestSchema_Default(t *testing.T) {
schema := Schema{
Type: NewSchemaType("integer"),
Default: 10,
}
if schema.Default != 10 {
t.Errorf("expected default 10, got %v", schema.Default)
}
}
func TestSchema_Const(t *testing.T) {
schema := Schema{
Type: NewSchemaType("string"),
Const: "fixed_value",
}
if schema.Const != "fixed_value" {
t.Errorf("expected const 'fixed_value', got %v", schema.Const)
}
}
func TestHeader(t *testing.T) {
header := Header{
Description: "Request ID",
Required: true,
Schema: &Schema{Type: NewSchemaType("string"), Format: "uuid"},
}
if header.Description != "Request ID" {
t.Errorf("expected description 'Request ID', got %q", header.Description)
}
if !header.Required {
t.Error("expected required to be true")
}
if header.Schema.Format != "uuid" {
t.Errorf("expected schema format 'uuid', got %q", header.Schema.Format)
}
}
func TestExample(t *testing.T) {
example := Example{
Summary: "Success example",
Description: "A successful response",
Value: map[string]any{
"id": 123,
"name": "Test User",
},
}
if example.Summary != "Success example" {
t.Errorf("expected summary 'Success example', got %q", example.Summary)
}
if example.Value == nil {
t.Error("expected value to be set")
}
}
func TestLink(t *testing.T) {
link := Link{
OperationID: "getUserById",
Parameters: map[string]any{
"userId": "$response.body#/id",
},
Description: "Get user by ID from response",
}
if link.OperationID != "getUserById" {
t.Errorf("expected operation ID 'getUserById', got %q", link.OperationID)
}
if len(link.Parameters) != 1 {
t.Errorf("expected 1 parameter, got %d", len(link.Parameters))
}
}
func TestCallback(t *testing.T) {
callback := Callback{
"{$request.body#/callbackUrl}": PathItem{
Post: &Operation{
Summary: "Webhook notification",
RequestBody: &RequestBody{
Content: map[string]MediaType{
"application/json": {
Schema: &Schema{Type: NewSchemaType("object")},
},
},
},
Responses: map[string]Response{
"200": {Description: "Acknowledged"},
},
},
},
}
if len(callback) != 1 {
t.Errorf("expected 1 callback path, got %d", len(callback))
}
}
func TestDiscriminator(t *testing.T) {
disc := Discriminator{
PropertyName: "type",
Mapping: map[string]string{
"user": "#/components/schemas/User",
"admin": "#/components/schemas/Admin",
},
}
if disc.PropertyName != "type" {
t.Errorf("expected property name 'type', got %q", disc.PropertyName)
}
if len(disc.Mapping) != 2 {
t.Errorf("expected 2 mappings, got %d", len(disc.Mapping))
}
}
func TestXML(t *testing.T) {
xml := XML{
Name: "User",
Namespace: "http://example.com/schema",
Prefix: "ex",
Wrapped: true,
}
if xml.Name != "User" {
t.Errorf("expected name 'User', got %q", xml.Name)
}
if xml.Namespace != "http://example.com/schema" {
t.Errorf("expected namespace 'http://example.com/schema', got %q", xml.Namespace)
}
if !xml.Wrapped {
t.Error("expected wrapped to be true")
}
}
func TestLicense(t *testing.T) {
license := License{
Name: "Apache 2.0",
URL: "https://www.apache.org/licenses/LICENSE-2.0.html",
}
if license.Name != "Apache 2.0" {
t.Errorf("expected name 'Apache 2.0', got %q", license.Name)
}
if license.URL != "https://www.apache.org/licenses/LICENSE-2.0.html" {
t.Errorf("expected url 'https://www.apache.org/licenses/LICENSE-2.0.html', got %q", license.URL)
}
}
func TestServerVariable(t *testing.T) {
variable := ServerVariable{
Enum: []string{"v1", "v2", "v3"},
Default: "v2",
Description: "API version",
}
if variable.Default != "v2" {
t.Errorf("expected default 'v2', got %q", variable.Default)
}
if len(variable.Enum) != 3 {
t.Errorf("expected 3 enum values, got %d", len(variable.Enum))
}
}
func TestEncoding(t *testing.T) {
encoding := Encoding{
ContentType: "application/json",
Headers: map[string]*Header{
"X-Custom": {
Schema: &Schema{Type: NewSchemaType("string")},
},
},
}
if encoding.ContentType != "application/json" {
t.Errorf("expected content type 'application/json', got %q", encoding.ContentType)
}
if len(encoding.Headers) != 1 {
t.Errorf("expected 1 header, got %d", len(encoding.Headers))
}
}
func TestComponents_Extended(t *testing.T) {
components := Components{
SecuritySchemes: map[string]*SecurityScheme{
"bearerAuth": {
Type: "http",
Scheme: "bearer",
},
},
Parameters: map[string]*Parameter{
"PageParam": {
Name: "page",
In: "query",
Schema: &Schema{Type: NewSchemaType("integer")},
},
},
RequestBodies: map[string]*RequestBody{
"UserBody": {
Required: true,
Content: map[string]MediaType{
"application/json": {
Schema: &Schema{Ref: "#/components/schemas/User"},
},
},
},
},
Links: map[string]*Link{
"GetUserByUserId": {
OperationID: "getUser",
},
},
}
if len(components.SecuritySchemes) != 1 {
t.Errorf("expected 1 security scheme, got %d", len(components.SecuritySchemes))
}
if len(components.Parameters) != 1 {
t.Errorf("expected 1 parameter, got %d", len(components.Parameters))
}
if len(components.RequestBodies) != 1 {
t.Errorf("expected 1 request body, got %d", len(components.RequestBodies))
}
if len(components.Links) != 1 {
t.Errorf("expected 1 link, got %d", len(components.Links))
}
}