| 1 |
// Copyright 2023-2024 Princess Beef Heavy Industries, LLC / Dave Shanley |
| 2 |
// https://pb33f.io |
| 3 |
|
| 4 |
package helpers |
| 5 |
|
| 6 |
import ( |
| 7 |
"mime" |
| 8 |
"net/http" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/pb33f/libopenapi/datamodel/high/v3" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
) |
| 14 |
|
| 15 |
// Test ExtractOperation for each HTTP method |
| 16 |
func TestExtractOperation(t *testing.T) { |
| 17 |
pathItem := &v3.PathItem{ |
| 18 |
Get: &v3.Operation{Summary: "GET operation"}, |
| 19 |
Post: &v3.Operation{Summary: "POST operation"}, |
| 20 |
Put: &v3.Operation{Summary: "PUT operation"}, |
| 21 |
Delete: &v3.Operation{Summary: "DELETE operation"}, |
| 22 |
Options: &v3.Operation{Summary: "OPTIONS operation"}, |
| 23 |
Head: &v3.Operation{Summary: "HEAD operation"}, |
| 24 |
Patch: &v3.Operation{Summary: "PATCH operation"}, |
| 25 |
Trace: &v3.Operation{Summary: "TRACE operation"}, |
| 26 |
} |
| 27 |
|
| 28 |
// Test all HTTP methods |
| 29 |
tests := []struct { |
| 30 |
method string |
| 31 |
want string |
| 32 |
}{ |
| 33 |
{http.MethodGet, "GET operation"}, |
| 34 |
{http.MethodPost, "POST operation"}, |
| 35 |
{http.MethodPut, "PUT operation"}, |
| 36 |
{http.MethodDelete, "DELETE operation"}, |
| 37 |
{http.MethodOptions, "OPTIONS operation"}, |
| 38 |
{http.MethodHead, "HEAD operation"}, |
| 39 |
{http.MethodPatch, "PATCH operation"}, |
| 40 |
{http.MethodTrace, "TRACE operation"}, |
| 41 |
} |
| 42 |
|
| 43 |
for _, tt := range tests { |
| 44 |
req, _ := http.NewRequest(tt.method, "/", nil) |
| 45 |
operation := ExtractOperation(req, pathItem) |
| 46 |
require.NotNil(t, operation) |
| 47 |
require.Equal(t, tt.want, operation.Summary) |
| 48 |
} |
| 49 |
|
| 50 |
// Test an unsupported HTTP method |
| 51 |
req, _ := http.NewRequest("INVALID", "/", nil) |
| 52 |
operation := ExtractOperation(req, pathItem) |
| 53 |
require.Nil(t, operation) |
| 54 |
} |
| 55 |
|
| 56 |
// Test ExtractContentType for various input cases |
| 57 |
func TestExtractContentType(t *testing.T) { |
| 58 |
// Simple content type with no charset or boundary |
| 59 |
contentType, charset, boundary := ExtractContentType("application/json") |
| 60 |
require.Equal(t, "application/json", contentType) |
| 61 |
require.Empty(t, charset) |
| 62 |
require.Empty(t, boundary) |
| 63 |
|
| 64 |
// Content type with charset |
| 65 |
contentType, charset, boundary = ExtractContentType("text/html; charset=UTF-8") |
| 66 |
require.Equal(t, "text/html", contentType) |
| 67 |
require.Equal(t, "UTF-8", charset) |
| 68 |
require.Empty(t, boundary) |
| 69 |
|
| 70 |
// Content type with boundary |
| 71 |
contentType, charset, boundary = ExtractContentType("multipart/form-data; boundary=----WebKitFormBoundary") |
| 72 |
require.Equal(t, "multipart/form-data", contentType) |
| 73 |
require.Empty(t, charset) |
| 74 |
require.Equal(t, "----WebKitFormBoundary", boundary) |
| 75 |
|
| 76 |
// Content type with both charset and boundary |
| 77 |
contentType, charset, boundary = ExtractContentType("multipart/form-data; charset=UTF-8; boundary=----WebKitFormBoundary") |
| 78 |
require.Equal(t, "multipart/form-data", contentType) |
| 79 |
require.Equal(t, "UTF-8", charset) |
| 80 |
require.Equal(t, "----WebKitFormBoundary", boundary) |
| 81 |
|
| 82 |
// Content type with leading/trailing spaces |
| 83 |
contentType, charset, boundary = ExtractContentType(" application/xml ; charset=ISO-8859-1 ; boundary=myBoundary ") |
| 84 |
require.Equal(t, "application/xml", contentType) |
| 85 |
require.Equal(t, "ISO-8859-1", charset) |
| 86 |
require.Equal(t, "myBoundary", boundary) |
| 87 |
|
| 88 |
// Invalid content type (no key-value pair for charset/boundary) |
| 89 |
contentType, charset, boundary = ExtractContentType("application/xml; charset; boundary") |
| 90 |
require.Equal(t, "application/xml", contentType) |
| 91 |
require.Empty(t, charset) |
| 92 |
require.Empty(t, boundary) |
| 93 |
|
| 94 |
// Content type with custom parameter |
| 95 |
contentType, charset, boundary = ExtractContentType("text/html; version=2") |
| 96 |
require.Equal(t, "text/html", contentType) |
| 97 |
require.Empty(t, charset) |
| 98 |
require.Empty(t, boundary) |
| 99 |
|
| 100 |
// Content type with custom parameter, charset, and boundary |
| 101 |
contentType, charset, boundary = ExtractContentType("text/html; charset=UTF-8; version=2; boundary=myBoundary") |
| 102 |
require.Equal(t, "text/html", contentType) |
| 103 |
require.Equal(t, "UTF-8", charset) |
| 104 |
require.Equal(t, "myBoundary", boundary) |
| 105 |
|
| 106 |
// mime.ParseMediaType returns an error, but ExtractContentType still returns the content type. |
| 107 |
const ct = "text/plain;;" |
| 108 |
_, _, err := mime.ParseMediaType(ct) |
| 109 |
require.ErrorIs(t, err, mime.ErrInvalidMediaParameter) |
| 110 |
contentType, charset, boundary = ExtractContentType(ct) |
| 111 |
require.Equal(t, "text/plain", contentType) |
| 112 |
require.Empty(t, charset) |
| 113 |
require.Empty(t, boundary) |
| 114 |
} |
| 115 |
|
| 116 |
func TestExtractOperationHeadFallback(t *testing.T) { |
| 117 |
pathItem := &v3.PathItem{ |
| 118 |
Get: &v3.Operation{Summary: "GET operation"}, |
| 119 |
Head: nil, |
| 120 |
} |
| 121 |
|
| 122 |
req, _ := http.NewRequest(http.MethodHead, "/", nil) |
| 123 |
operation := ExtractOperation(req, pathItem) |
| 124 |
require.NotNil(t, operation) |
| 125 |
require.Equal(t, "GET operation", operation.Summary) |
| 126 |
} |
| 127 |
|
| 128 |
func TestExtractOperationHeadFallbackNoGet(t *testing.T) { |
| 129 |
pathItem := &v3.PathItem{ |
| 130 |
Head: nil, |
| 131 |
Get: nil, |
| 132 |
} |
| 133 |
|
| 134 |
req, _ := http.NewRequest(http.MethodHead, "/", nil) |
| 135 |
operation := ExtractOperation(req, pathItem) |
| 136 |
require.Nil(t, operation) |
| 137 |
} |