Cannot set middleware

45 views
Skip to first unread message

Alex Soto

unread,
Jan 23, 2018, 6:04:12 AM1/23/18
to hoverfly

Hello, I am using Hoverfly v0.15.0 and want to use middleware to modify some data.

Currently my file looks like:

#!/usr/bin/env python

import sys
import json
import logging
import random
import base64
import os
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC

logging.basicConfig(filename='middleware.log', level=logging.DEBUG)
logging.debug('Middleware "modify_request" called')

def main():
    payload = sys.stdin.readlines()[0]

    logging.debug(payload)

    payload_dict = json.loads(payload)
    t = payload_dict['data']['pairs'][0]['request']['headers']['Authorization'][0]

    if "response" in payload_dict and "body" in payload_dict["response"]:
        payload_dict["response"]["body"] = "{'foo': 'baz'}\n"

    print(json.dumps(payload_dict))

if __name__ == "__main__":
    main()

But when I run hoverctl middleware --binary python --script middleware.py I got:

Could not set middleware

Invalid middleware: exit status 1

But if I remove the line  t = payload_dict['data']['pairs'][0]['request']['headers']['Authorization'][0] then it works. I am not an expert of Python but I have executed this script from a main (passing me the string) and it worked so I am not sure where it is the problem.

Any idea?

Tank you very much.




Tommy Situ

unread,
Jan 23, 2018, 7:29:09 AM1/23/18
to Alex Soto, hoverfly
Hi Alex, 

Whenever you get an error saying "Invalid middleware”, it actually means that your middleware doesn’t pass the test :(

So hoverfly tries to test your middleware with this Request-Response pair: 

testData := models.RequestResponsePair{
Request: models.RequestDetails{
Path: "/",
Method: "GET",
Destination: "www.test.com",
Scheme: "",
Query: map[string][]string{},
Body: "",
Headers: map[string][]string{"test_header": []string{"true"}},
},
Response: models.ResponseDetails{
Status: 200,
Body: "ok",
Headers: map[string][]string{"test_header": []string{"true"}},
},
}

And this line in your script: 
t = payload_dict['data']['pairs'][0]['request']['headers']['Authorization'][0]

will fail because the Authorisation header does not exist in the test data. 

My suggestion would be adding some error handling in your script. 


We should be able to improve the logging and documentation around middleware in the next release.


Best regards, 

Tommy Situ
Software Engineer

Twitter: @c2tommy / @spectolabs
Web: specto.io



--
You received this message because you are subscribed to the Google Groups "hoverfly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hoverfly+u...@specto.io.
To post to this group, send email to hove...@specto.io.
To view this discussion on the web visit https://groups.google.com/a/specto.io/d/msgid/hoverfly/a5a641d5-60e4-45a5-9564-745b14e76e22%40specto.io.

Alex Soto

unread,
Jan 23, 2018, 7:32:58 AM1/23/18
to hoverfly, aso...@gmail.com
Thank you very much, yes now I was figuring out that something like that would be happening, now I was to ignore parsing errors, but yeah definitely if the examples provided in docs could add this checks would be awesome.

Thank you very much. 

El dimarts, 23 gener de 2018 13:29:09 UTC+1, Tommy Situ va escriure:
Reply all
Reply to author
Forward
0 new messages