Skip to content

Commit

Permalink
Fix Content-Type custom payload bugs (#888)
Browse files Browse the repository at this point in the history
* Fix Content-Type custom payload bugs

When a custom payload body is used to work around unsupported content types, the Content-Type
header must also be modified via the dictionary.  This functionality had several bugs, which
are fixed in this change.  After this update, using either `restler_custom_payload_header` or
`restler_custom_payload` with the request-specific syntax for dictionary payloads now works
as expected, for example:

  "restler_custom_payload": {
    "/stores/{storeId}/order/post/Content-Type": [ "xml" ]
  }

This change also fixes the same bugs with per-request syntax with restler_custom_payload_query.

Testing:
- manual testing with demo_server
- added new compiler tests

* Fix prefix computation per PR feedback
  • Loading branch information
marina-p committed Apr 16, 2024
1 parent 2cbb2a8 commit c75e59e
Show file tree
Hide file tree
Showing 11 changed files with 1,513 additions and 36 deletions.
52 changes: 42 additions & 10 deletions src/compiler/Restler.Compiler.Test/DictionaryTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,15 @@ module Dictionary =
Assert.True((numFuzzableStrings2 = 1))

[<Fact>]
/// Test that you can replace the content type of the request payload
/// Test replacing the content type of the request payload
/// The test checks 3 paths:
/// - /stores/{storeId}/order - body of type json, query parameters, no headers.
/// - /stores/{storeId}/order2 - body of type json, headers, no Content-Type header defined.
/// - /stores/{storeId}/order3 - body of type json, with Content-Type header defined.
/// - /stores/{storeId}/order4 - body of type json, with Content-Type header defined.
/// The first three cases will have content-type defined in custom payload and custom payload header (in
/// different dictionary tests). The last one will not have any custom payload and Content-Type should be json.
/// The results are compared against a grammar baseline.
let ``content type can be modified via custom_payload_header`` () =
let grammarOutputDirPath = ctx.testRootDirPath
let config = { Restler.Config.SampleConfig with
Expand All @@ -136,18 +144,42 @@ module Dictionary =
ResolveQueryDependencies = true
UseBodyExamples = Some false
UseQueryExamples = Some false
SwaggerSpecFilePath = Some [(Path.Combine(Environment.CurrentDirectory, "swagger", "dictionaryTests", "customPayloadSwagger.json"))]
CustomDictionaryFilePath = Some (Path.Combine(Environment.CurrentDirectory, "swagger", "dictionaryTests", "customPayloadRequestTypeDict.json"))
SwaggerSpecFilePath = Some [(Path.Combine(Environment.CurrentDirectory, "swagger", "dictionaryTests",
"customPayloadContentTypeSwagger.json"))]
CustomDictionaryFilePath = None
}
Restler.Workflow.generateRestlerGrammar config
let runTest dictionaryFilePath expectedGrammarFilePath =
// Now test the same spec with custom payload headers in the dictionary
let config = {config with
CustomDictionaryFilePath = dictionaryFilePath }

Restler.Workflow.generateRestlerGrammar config

let actualGrammarFilePath = Path.Combine(grammarOutputDirPath,
Restler.Workflow.Constants.DefaultRestlerGrammarFileName)
let grammarDiff = getLineDifferences expectedGrammarFilePath actualGrammarFilePath
let message = sprintf "Grammar Does not match baseline. First difference: %A" grammarDiff
Assert.True(grammarDiff.IsNone, message)

//// test restler_custom_payload
let customDictionaryFilePath = Some (Path.Combine(Environment.CurrentDirectory, "swagger", "dictionaryTests",
"customPayloadRequestTypeDict.json"))

let expectedGrammarFilePath = Path.Combine(Environment.CurrentDirectory,
"baselines", "dictionaryTests", "customPayloadContentType_grammar.py")
runTest customDictionaryFilePath expectedGrammarFilePath

// test restler_custom_payload_header
let customDictionaryFilePath = Some (Path.Combine(Environment.CurrentDirectory, "swagger", "dictionaryTests",
"customPayloadHeaderRequestTypeDict.json"))

let expectedGrammarFilePath = Path.Combine(Environment.CurrentDirectory,
"baselines", "dictionaryTests", "customPayloadHeaderContentType_grammar.py")
runTest customDictionaryFilePath expectedGrammarFilePath



let grammarFilePath = Path.Combine(grammarOutputDirPath,
Restler.Workflow.Constants.DefaultRestlerGrammarFileName)
let grammar = File.ReadAllText(grammarFilePath)

Assert.True(grammar.Contains("""primitives.restler_static_string("Content-Type: "),"""))
Assert.True(grammar.Contains("""primitives.restler_static_string("application/json"),"""))
Assert.True(grammar.Contains("""primitives.restler_custom_payload("/stores/{storeId}/order/post/Content-Type", quoted=False),"""))

[<Fact>]
/// Baseline test for the dynamic value generators template
Expand Down
18 changes: 15 additions & 3 deletions src/compiler/Restler.Compiler.Test/Restler.Compiler.Test.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@
<Content Include="baselines\dictionaryTests\customPayloadDict_ValueGeneratorTemplate.py">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="baselines\exampleTests\array_example_grammar.py">
<Content Include="baselines\dictionaryTests\customPayloadContentType_grammar.py">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="baselines\dictionaryTests\customPayloadHeaderContentType_grammar.py">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="baselines\exampleTests\array_example_grammar.py">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="swagger\exampleTests\example_int_openapi3.yml">
Expand Down Expand Up @@ -235,13 +241,19 @@
<Content Include="swagger\dictionaryTests\customPayloadSwagger.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="swagger\dictionaryTests\customPayloadDict.json">
<Content Include="swagger\dictionaryTests\customPayloadContentTypeSwagger.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="swagger\dictionaryTests\customPayloadDict.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="swagger\dictionaryTests\customPayloadRequestTypeDict.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="swagger\dictionaryTests\serializationTestDict.json">
<Content Include="swagger\dictionaryTests\customPayloadHeaderRequestTypeDict.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="swagger\dictionaryTests\serializationTestDict.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="swagger\dependencyTests\lowercase_paths.json">
Expand Down

0 comments on commit c75e59e

Please sign in to comment.