Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CEL-based rules for dynamically generating stub data #17

Merged
merged 20 commits into from
Dec 5, 2024
Merged

Conversation

sudorandom
Copy link
Owner

@sudorandom sudorandom commented Dec 4, 2024

Changes

Resolves #9

  • Adds an "--only-stubs" option so fauxrpc will return an error is there is no stub registered for the method or type
  • Improves the behavior of stub database: now stubs registered to a method will not just register for the output type. This could be confusing and wrong for those who reuse output types.
  • Adds support for loading stubs from a path. They are defined in JSON or YAML.
  • Adds stub priority 0-100, higher priority will be considered before older ones
  • Adds active_if CEL expression that will trigger a stub to be used
  • Adds some examples of stub files

Examples

Return a static response:

---
stubs:
- id: find-pets-by-status
  target: io.swagger.petstore.v2.PetService/FindPetsByStatus
  content:
    pets:
    - id: 1
      category:
        id: 1
        name: cat
      name: Whiskers
      photo_urls:
      - https://cataas.com/cat
      tags:
      - id: 1
        name: cute
      - id: 2
        name: kid-friendly
      status: available

Return nothing:

---
stubs:
- id: delete-pet-empty
  target: io.swagger.petstore.v2.PetService/DeletePet
  content: {}

Return a static stub if the "pet_id" is 1, otherwise return a randomly generated stub:

---
stubs:
- id: get-pets-by-id-id-1
  target: io.swagger.petstore.v2.PetService/GetPetByID
  active_if: req.pet_id == 1
  priority: 100
  content:
    id: 1
    category:
      id: 1
      name: cat
    name: Whiskers
    photo_urls:
    - https://cataas.com/cat
    tags:
    - id: 1
      name: cute
    - id: 2
      name: kid-friendly
    status: available
- id: get-pets-by-id-default
  target: io.swagger.petstore.v2.PetService/GetPetByID
  cel_content: |
    {
        'id': req.pet_id,
        'category': {'id': gen, 'name': 'gen'},
        'name': gen,
        'photo_urls': [gen, gen],
        'tags': [{'id': gen, 'name': gen}],
        'status': gen
    }

Simply return the request as the response, (works when the req and resp are the same type):

---
stubs:
- id: add-pet
  target: io.swagger.petstore.v2.PetService/AddPet
  cel_content: req

@sudorandom sudorandom merged commit 70e9c3c into main Dec 5, 2024
3 checks passed
@sudorandom sudorandom deleted the cel-rules branch December 5, 2024 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add CEL rules for stubs
1 participant