cdent / gabbi

Declarative HTTP Testing for Python and anything else
http://gabbi.readthedocs.org/
Other
148 stars 34 forks source link

270 replacer casts #275

Closed cdent closed 3 years ago

cdent commented 5 years ago

This is an effort to provide the functionality described in #270 by implementing optional "casts" on the $ENVIRON and $REPLACER replacers by appending a :cast (int, str, bool, float) to the replacer name (e.g., $ENVIRON:int['FOOBAR']).

This only works when the replacer is the entirety of the message being replaced:

data:
    foo: $ENVIRON:int['FOOBAR']

not mixed in:

data:
    foo: boom $ENVIRON:int['FOOBAR'] boom

We can't do this for two reasons:

While the code is present to make it possible to do casts of other replacers, for the moment any cast will be ignored, mostly because it doesn't make sense for most of them.

It might, however, make sense for $COOKIE so input on that desired.

This isn't done, it needs to be confirmed as having grammatical sense, and it also needs doc update, but I'm putting it up here for some initial feedback on the format and implementation.

cdent commented 5 years ago

@jd I suspect you might have some input on this if you can cast (ha!) your mind back to gnocchi time.

jd commented 5 years ago

I think we mostly used ENVIRON in headers not in data, so we were cool with strings only.

Now I understand why you'd need this feature in data. I did not check the implementation, but the principle LGTM.

cdent commented 5 years ago

@taget have you had a chance to try this out?

cdent commented 4 years ago

@taget I'm still hoping you can verify that this solves the problems you were trying to solve

taget commented 4 years ago

@cdent hi Chris, sorry to not response you for long time.

I tried in my test environment, works for me when cast a integer to string

yaml:

  POST: /api/cgi
  request_headers:
    content-type: application/json
  data:
    params:
      content:
        requestInfo:
          requestKey: mx_ksdionuj3djfk
          caller: vs-api
          seqId: ''
          requestId: ''
        version: '1.0'
        type: json
        requestItem:
          data:
            detail:
            - uuid: $ENVIRON['VS_UUID']
              hostIpList:
                  - $ENVIRON['VS_HOSTIP']
              migrateBandwidth: 20
              migrateTimeout: 3600
              pauseTime: $ENVIRON:str['PAUSE_TIME']
              schedulerDebug: 'on'
              compression:
                compression: mt
          method: instance_live_migrate

VS_HOSTIP="101" VS_UUID="100" PAUSE_TIME=122 gabbi-run http://127.0.0.1:8000 < a.yaml

result

{
  "params": {
    "content": {
      "type": "json",
      "requestInfo": {
        "seqId": "",
        "requestId": "",
        "caller": "vs-api",
        "requestKey": "mx_ksdionuj3djfk"
      },
      "requestItem": {
        "method": "instance_live_migrate",
        "data": {
          "detail": [
            {
              "schedulerDebug": "on",
              "migrateBandwidth": 20,
              "uuid": 100,
              "compression": {
                "compression": "mt"
              },
              "pauseTime": "122",
              "migrateTimeout": 3600,
              "hostIpList": [
                101
              ]
            }
          ]
        }
      },
      "version": "1.0"
    }
  }
}

thanks for your help :)