Skip to content

Commit

Permalink
feat(soap): adds additional script method and configuration property …
Browse files Browse the repository at this point in the history
…to return a fault.
  • Loading branch information
outofcoffee committed Oct 19, 2024
1 parent 32d5e34 commit 6725f31
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ interface MutableResponseBehaviour {
@Deprecated("Use withContent(String) instead", replaceWith = ReplaceWith("withContent"))
fun withData(responseData: String?) = withContent(responseData)
fun template(): MutableResponseBehaviour
fun withExampleName(exampleName: String): MutableResponseBehaviour
fun usingDefaultBehaviour(): MutableResponseBehaviour
fun skipDefaultBehaviour(): MutableResponseBehaviour
fun continueToNext(): MutableResponseBehaviour
Expand All @@ -69,6 +68,16 @@ interface MutableResponseBehaviour {
fun withFailure(failureType: String): MutableResponseBehaviour
fun withFailureType(failureType: FailureSimulationType?): MutableResponseBehaviour

/**
* Only supported for OpenAPI plugin.
*/
fun withExampleName(exampleName: String): MutableResponseBehaviour

/**
* Only supported for SOAP plugin.
*/
fun withSoapFault(): MutableResponseBehaviour

@Deprecated("Use skipDefaultBehaviour() instead", ReplaceWith("skipDefaultBehaviour()"))
fun immediately(): MutableResponseBehaviour
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ open class ReadWriteResponseBehaviourImpl : ReadWriteResponseBehaviour {
override var responseFile: String? = null
override var content: String? = null
override var isTemplate = false
override var exampleName: String? = null
override val responseHeaders: MutableMap<String, String> = mutableMapOf()
private var behaviourConfigured = false
override var performanceSimulation: PerformanceSimulationConfig? = null
override var failureType: FailureSimulationType? = null
override var exampleName: String? = null
override var soapFault: Boolean = false

override fun template(): MutableResponseBehaviour {
isTemplate = true
Expand Down Expand Up @@ -108,11 +109,6 @@ open class ReadWriteResponseBehaviourImpl : ReadWriteResponseBehaviour {
return this
}

override fun withExampleName(exampleName: String): MutableResponseBehaviour {
this.exampleName = exampleName
return this
}

/**
* Use the plugin's default behaviour to respond
*
Expand Down Expand Up @@ -205,4 +201,15 @@ open class ReadWriteResponseBehaviourImpl : ReadWriteResponseBehaviour {
this.failureType = failureType
return this
}

override fun withExampleName(exampleName: String): MutableResponseBehaviour {
this.exampleName = exampleName
return this
}

override fun withSoapFault(): MutableResponseBehaviour {
this.soapFault = true
withStatusCode(500)
return this
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,17 @@ interface ResponseBehaviour {
val responseFile: String?
val content: String?
val isTemplate: Boolean
val exampleName: String?
val behaviourType: ResponseBehaviourType?
val performanceSimulation: PerformanceSimulationConfig?
val failureType: FailureSimulationType?

/**
* Only supported for OpenAPI plugin.
*/
val exampleName: String?

/**
* Only supported for SOAP plugin.
*/
val soapFault: Boolean
}
12 changes: 10 additions & 2 deletions docs/soap_plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,13 @@ HTTP/1.1 400 Bad Request

## Returning fault messages

If your WSDL document defines a `fault`, then Imposter can generate a sample response from its type. To return a fault, set the response status code to 500.
If your WSDL document defines a `fault`, then Imposter can generate a sample response from its type.

To return a fault you can:

1. set the response status code to `500`, or
2. set the `response.soapFault` configuration property to `true`, or
3. use the `respond().withSoapFault()` script function

### Example configuration to respond with a fault

Expand All @@ -241,7 +247,8 @@ resources:
```

> **Tip**
> Use conditional matching with resources, to only return a fault in particular circumstances.
> Use conditional matching with resources, to only return a fault in particular circumstances.
> See [fault-example](https://github.com/outofcoffee/imposter/blob/main/examples/soap/fault-example) for an example of how to do this.

## Scripted responses (advanced)

Expand Down Expand Up @@ -312,6 +319,7 @@ Now, `example.groovy` can control the responses, such as:

- [conditional-example](https://github.com/outofcoffee/imposter/blob/main/examples/soap/conditional-example)
- [scripted-example](https://github.com/outofcoffee/imposter/blob/main/examples/soap/scripted-example)
- [fault-example](https://github.com/outofcoffee/imposter/blob/main/examples/soap/fault-example)

### Configuration reference

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ class OpenApiResponseBehaviourFactory : DefaultResponseBehaviourFactory() {
responseBehaviour.withExampleName(configExampleName!!)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
package io.gatehill.imposter.plugin.soap

import io.gatehill.imposter.ImposterConfig
import io.gatehill.imposter.http.DefaultResponseBehaviourFactory
import io.gatehill.imposter.http.DefaultStatusCodeFactory
import io.gatehill.imposter.http.HttpExchange
import io.gatehill.imposter.http.HttpMethod
Expand All @@ -53,6 +52,7 @@ import io.gatehill.imposter.plugin.RequireModules
import io.gatehill.imposter.plugin.config.ConfiguredPlugin
import io.gatehill.imposter.plugin.config.resource.BasicResourceConfig
import io.gatehill.imposter.plugin.soap.config.SoapPluginConfig
import io.gatehill.imposter.plugin.soap.http.SoapResponseBehaviourFactory
import io.gatehill.imposter.plugin.soap.model.BindingType
import io.gatehill.imposter.plugin.soap.model.MessageBodyHolder
import io.gatehill.imposter.plugin.soap.model.OperationMessage
Expand Down Expand Up @@ -97,6 +97,7 @@ class SoapPluginImpl @Inject constructor(
private val responseService: ResponseService,
private val responseRoutingService: ResponseRoutingService,
private val soapExampleService: SoapExampleService,
private val soapResponseBehaviourFactory: SoapResponseBehaviourFactory,
) : ConfiguredPlugin<SoapPluginConfig>(
vertx, imposterConfig
) {
Expand Down Expand Up @@ -224,7 +225,6 @@ class SoapPluginImpl @Inject constructor(
soapAction: String?,
): CompletableFuture<Unit> {
val statusCodeFactory = DefaultStatusCodeFactory.instance
val responseBehaviourFactory = DefaultResponseBehaviourFactory.instance
val resourceConfig = httpExchange.get<BasicResourceConfig>(ResourceUtil.RESOURCE_CONFIG_KEY)

val defaultBehaviourHandler: DefaultBehaviourHandler = { responseBehaviour: ResponseBehaviour ->
Expand Down Expand Up @@ -290,7 +290,7 @@ class SoapPluginImpl @Inject constructor(
httpExchange,
context,
statusCodeFactory,
responseBehaviourFactory,
soapResponseBehaviourFactory,
defaultBehaviourHandler,
)
}
Expand All @@ -299,8 +299,9 @@ class SoapPluginImpl @Inject constructor(
responseBehaviour: ResponseBehaviour,
operation: WsdlOperation
): OperationMessage? {
return when (responseBehaviour.statusCode) {
HttpUtil.HTTP_INTERNAL_ERROR -> operation.faultRef
return when {
responseBehaviour.soapFault -> operation.faultRef
responseBehaviour.statusCode == HttpUtil.HTTP_INTERNAL_ERROR -> operation.faultRef
else -> operation.outputRef
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ class SoapPluginConfig : CommonPluginConfig(), ResourcesHolder<SoapPluginResourc
@JsonProperty("envelope")
val envelope: Boolean = true

@JsonProperty("response")
override val responseConfig = SoapResponseConfig()

override fun toString(): String {
return "SoapPluginConfig(parent=${super.toString()}, wsdlFile=$wsdlFile, resources=$resources, isDefaultsFromRootResponse=$isDefaultsFromRootResponse, envelope=$envelope)"
return "SoapPluginConfig(parent=${super.toString()}, wsdlFile=$wsdlFile, resources=$resources, isDefaultsFromRootResponse=$isDefaultsFromRootResponse, envelope=$envelope, responseConfig=$responseConfig)"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ import io.gatehill.imposter.plugin.config.resource.AbstractResourceConfig
import io.gatehill.imposter.plugin.config.resource.EvalResourceConfig
import io.gatehill.imposter.plugin.config.resource.request.RequestBodyConfig
import io.gatehill.imposter.plugin.config.resource.request.RequestBodyResourceConfig
import io.gatehill.imposter.plugin.config.steps.StepConfig
import io.gatehill.imposter.plugin.config.steps.StepsConfigHolder
import java.util.*

class SoapPluginResourceConfig : AbstractResourceConfig(), RequestBodyResourceConfig, EvalResourceConfig {
class SoapPluginResourceConfig : AbstractResourceConfig(), RequestBodyResourceConfig, EvalResourceConfig, StepsConfigHolder {
@JsonProperty("binding")
val binding: String? = null

Expand All @@ -67,6 +69,12 @@ class SoapPluginResourceConfig : AbstractResourceConfig(), RequestBodyResourceCo
@field:JsonProperty("eval")
override var eval: String? = null

@field:JsonProperty("steps")
override val steps: List<StepConfig>? = null

@JsonProperty("response")
override val responseConfig = SoapResponseConfig()

@get:JsonIgnore
override val resourceId by lazy { UUID.randomUUID().toString() }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2016-2021.
*
* This file is part of Imposter.
*
* "Commons Clause" License Condition v1.0
*
* The Software is provided to you by the Licensor under the License, as
* defined below, subject to the following condition.
*
* Without limiting other conditions in the License, the grant of rights
* under the License will not include, and the License does not grant to
* you, the right to Sell the Software.
*
* For purposes of the foregoing, "Sell" means practicing any or all of
* the rights granted to you under the License to provide to third parties,
* for a fee or other consideration (including without limitation fees for
* hosting or consulting/support services related to the Software), a
* product or service whose value derives, entirely or substantially, from
* the functionality of the Software. Any license notice or attribution
* required by the License must also include this Commons Clause License
* Condition notice.
*
* Software: Imposter
*
* License: GNU Lesser General Public License version 3
*
* Licensor: Peter Cornish
*
* Imposter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Imposter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Imposter. If not, see <https://www.gnu.org/licenses/>.
*/
package io.gatehill.imposter.plugin.soap.config

import io.gatehill.imposter.plugin.config.resource.ResponseConfig

/**
* Extends the base response configuration with items specific
* to the OpenAPI plugin.
*
* @author Pete Cornish
*/
class SoapResponseConfig : ResponseConfig() {
val soapFault: Boolean? = null

override fun hasConfiguration(): Boolean =
super.hasConfiguration() || null != soapFault

override fun toString(): String {
return "OpenApiResponseConfig(parent=${super.toString()}, soapFault=$soapFault)"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2016-2021.
*
* This file is part of Imposter.
*
* "Commons Clause" License Condition v1.0
*
* The Software is provided to you by the Licensor under the License, as
* defined below, subject to the following condition.
*
* Without limiting other conditions in the License, the grant of rights
* under the License will not include, and the License does not grant to
* you, the right to Sell the Software.
*
* For purposes of the foregoing, "Sell" means practicing any or all of
* the rights granted to you under the License to provide to third parties,
* for a fee or other consideration (including without limitation fees for
* hosting or consulting/support services related to the Software), a
* product or service whose value derives, entirely or substantially, from
* the functionality of the Software. Any license notice or attribution
* required by the License must also include this Commons Clause License
* Condition notice.
*
* Software: Imposter
*
* License: GNU Lesser General Public License version 3
*
* Licensor: Peter Cornish
*
* Imposter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Imposter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Imposter. If not, see <https://www.gnu.org/licenses/>.
*/
package io.gatehill.imposter.plugin.soap.http

import io.gatehill.imposter.http.DefaultResponseBehaviourFactory
import io.gatehill.imposter.plugin.config.resource.BasicResourceConfig
import io.gatehill.imposter.plugin.soap.config.SoapResponseConfig
import io.gatehill.imposter.script.ReadWriteResponseBehaviour

/**
* Extends base response behaviour population with specific
* SOAP plugin configuration.
*
* @author Pete Cornish
*/
class SoapResponseBehaviourFactory : DefaultResponseBehaviourFactory() {
override fun populate(
statusCode: Int,
resourceConfig: BasicResourceConfig,
responseBehaviour: ReadWriteResponseBehaviour
) {
if ((resourceConfig.responseConfig as SoapResponseConfig).soapFault == true) {
responseBehaviour.withSoapFault()
}

// invoke superclass after calling `withSoapFault` as it
// overrides the status code to 500
super.populate(statusCode, resourceConfig, responseBehaviour)
}
}
Loading

0 comments on commit 6725f31

Please sign in to comment.