Skip to content

Need a .respond().withRawContent() or a way to ignore some directory in the "auto-reload" #738

Description

@hilsonp

imposter: 4.7.0
plugin: rest
scripting: groovy

We are using groovy script to compose responses
resources:

  • method: POST
    response:
    scriptFile: request.retrieveDocument.groovy

In the request.retrieveDocument.groovy, we build a response that contain binary data (pdf). We MUST mimic our real provider so we cannot serve this Base64 encoded.

The way we do it is writing the bytes to a temporary file in the configDir, then respond with .withFile()
This works perfectly and the binary data is not corrupted.

    def currentDir = new File(getClass().protectionDomain.codeSource.location.path).parent + "/"
    def baos = new ByteArrayOutputStream()
    // here, we build the baos content... 
    def storageDir = new File("${currentDir}.temp_responses/")
    if (!storageDir.exists()) storageDir.mkdirs()

    // Deletes files in this folder older than 10 minutes to prevent disk bloat
    def now = System.currentTimeMillis()
    storageDir.listFiles().each { f ->
        if (now - f.lastModified() > 60000) f.delete()
    }

    def tempFile = File.createTempFile("invoice_resp_", ".bin", storageDir)
    tempFile.bytes = baos.toByteArray()
    logger.debug("Assembled multipart response saved to: ${tempFile.path}")

    //byte[] finalMultipartBody = baos.toByteArray()

    respond()
            .withStatusCode(httpStat)
            .withHeader('Content-Type', "multipart/related; boundary=\"${boundary}\"")
            .withFile(".temp_responses/" + tempFile.name)

The problem with this method is that we are writing in the configDir and therefore, imposter restarts.
We would like to keep the "auto-restart" as it makes our pipelines easier.

I tried writing the temp file somewere else than the configDir but got an security exception (which I fully understand).

We tried the .withContent() but this corrupts the content as it requires a String.

Would it be possible to add a .withContent() accepting byte[]
or accepting a .withRawContent() ?

Another solution would be to allow us to ignore some directories in the "auto-reload".

Any alternative is welcome.

Thank you very much.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions