Skip to content

Commit 07b06eb

Browse files
committed
upodated test to be more useful
1 parent 39485c1 commit 07b06eb

1 file changed

Lines changed: 67 additions & 32 deletions

File tree

validator_test.go

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,6 +2678,16 @@ paths:
26782678
application/json:
26792679
schema:
26802680
$ref: '#/components/schemas/Field'
2681+
post:
2682+
requestBody:
2683+
required: true
2684+
content:
2685+
application/json:
2686+
schema:
2687+
$ref: '#/components/schemas/Field'
2688+
responses:
2689+
'201':
2690+
description: Created
26812691
components:
26822692
schemas:
26832693
BooleanField:
@@ -2710,41 +2720,66 @@ components:
27102720
discriminator:
27112721
propertyName: field_type`
27122722

2713-
doc, err := libopenapi.NewDocument([]byte(spec))
2714-
require.NoError(t, err)
2715-
2716-
v, errs := NewValidator(doc)
2717-
require.Empty(t, errs)
2723+
// Subtest with cache enabled (exercises cache-warming paths in validator.go)
2724+
t.Run("cached", func(t *testing.T) {
2725+
doc, err := libopenapi.NewDocument([]byte(spec))
2726+
require.NoError(t, err)
2727+
2728+
v, errs := NewValidator(doc)
2729+
require.Empty(t, errs)
2730+
2731+
body, _ := json.Marshal(map[string]interface{}{
2732+
"field_type": "boolean",
2733+
"default_value": true,
2734+
})
2735+
request, _ := http.NewRequest(http.MethodGet, "https://example.com/fields", nil)
2736+
response := &http.Response{
2737+
StatusCode: 200,
2738+
Header: http.Header{"Content-Type": []string{"application/json"}},
2739+
Body: io.NopCloser(bytes.NewBuffer(body)),
2740+
}
27182741

2719-
// Valid BooleanField response
2720-
body, _ := json.Marshal(map[string]interface{}{
2721-
"field_type": "boolean",
2722-
"default_value": true,
2742+
valid, validationErrors := v.ValidateHttpResponse(request, response)
2743+
assert.True(t, valid, "response validation should pass for valid discriminator oneOf payload")
2744+
assert.Empty(t, validationErrors, "no validation errors expected")
27232745
})
2724-
request, _ := http.NewRequest(http.MethodGet, "https://example.com/fields", nil)
2725-
response := &http.Response{
2726-
StatusCode: 200,
2727-
Header: http.Header{"Content-Type": []string{"application/json"}},
2728-
Body: io.NopCloser(bytes.NewBuffer(body)),
2729-
}
27302746

2731-
valid, validationErrors := v.ValidateHttpResponse(request, response)
2732-
assert.True(t, valid, "response validation should pass for valid discriminator oneOf payload")
2733-
assert.Empty(t, validationErrors, "no validation errors expected")
2747+
// Subtest with cache disabled (exercises uncached compile paths in
2748+
// validate_response.go, validate_request.go)
2749+
t.Run("uncached", func(t *testing.T) {
2750+
doc, err := libopenapi.NewDocument([]byte(spec))
2751+
require.NoError(t, err)
2752+
2753+
v, errs := NewValidator(doc, config.WithSchemaCache(nil))
2754+
require.Empty(t, errs)
2755+
2756+
// Response validation (validate_response.go cache-miss path)
2757+
body, _ := json.Marshal(map[string]interface{}{
2758+
"field_type": "string",
2759+
"max_length": 255,
2760+
})
2761+
request, _ := http.NewRequest(http.MethodGet, "https://example.com/fields", nil)
2762+
response := &http.Response{
2763+
StatusCode: 200,
2764+
Header: http.Header{"Content-Type": []string{"application/json"}},
2765+
Body: io.NopCloser(bytes.NewBuffer(body)),
2766+
}
27342767

2735-
// Valid StringField response
2736-
body2, _ := json.Marshal(map[string]interface{}{
2737-
"field_type": "string",
2738-
"max_length": 255,
2768+
valid, validationErrors := v.ValidateHttpResponse(request, response)
2769+
assert.True(t, valid, "uncached response validation should pass")
2770+
assert.Empty(t, validationErrors, "no validation errors expected (uncached response)")
2771+
2772+
// Request validation (validate_request.go cache-miss path)
2773+
reqBody, _ := json.Marshal(map[string]interface{}{
2774+
"field_type": "boolean",
2775+
"default_value": false,
2776+
})
2777+
postReq, _ := http.NewRequest(http.MethodPost, "https://example.com/fields",
2778+
bytes.NewBuffer(reqBody))
2779+
postReq.Header.Set("Content-Type", "application/json")
2780+
2781+
validReq, reqErrors := v.ValidateHttpRequest(postReq)
2782+
assert.True(t, validReq, "uncached request validation should pass")
2783+
assert.Empty(t, reqErrors, "no validation errors expected (uncached request)")
27392784
})
2740-
request2, _ := http.NewRequest(http.MethodGet, "https://example.com/fields", nil)
2741-
response2 := &http.Response{
2742-
StatusCode: 200,
2743-
Header: http.Header{"Content-Type": []string{"application/json"}},
2744-
Body: io.NopCloser(bytes.NewBuffer(body2)),
2745-
}
2746-
2747-
valid2, validationErrors2 := v.ValidateHttpResponse(request2, response2)
2748-
assert.True(t, valid2, "response validation should pass for valid StringField payload")
2749-
assert.Empty(t, validationErrors2, "no validation errors expected for StringField")
27502785
}

0 commit comments

Comments
 (0)