dryvPacks

@dryv/k6-smoke-testsv0.1.0

k6 HTTP smoke/load script generated from canonical effective routes.

k6 Smoke TestsTestingUnitPublished 7 days ago2 downloads

dryv pack add @dryv/k6-smoke-tests
6.6 KBRaw ↗
jinja
import http from 'k6/http';
import { check, group } from 'k6';

export const options = {
  vus: 1,
  iterations: 1,
};

const BASE_URL = (__ENV.BASE_URL || {{ pack.inputs.base_url | tojson }}).replace(/\/$/, '');

{%- macro scalar_example(value) -%}
{%- if value is none -%}null
{%- elif value.is.enum and value.property.enum.values -%}{{ value.property.enum.values[0].value_json }}
{%- elif value.is.boolean -%}false
{%- elif value.is.integer -%}1
{%- elif value.is.number -%}1
{%- elif value.is.uuid -%}"00000000-0000-4000-8000-000000000001"
{%- elif value.is.email -%}"[email protected]"
{%- elif value.is.url or value.is.uri -%}"https://example.com"
{%- elif value.is.date -%}"2026-01-01"
{%- elif value.is.time -%}"12:00:00"
{%- elif value.is.temporal -%}"2026-01-01T12:00:00Z"
{%- elif value.is.record -%}{}
{%- else -%}"example"
{%- endif -%}
{%- endmacro -%}

{%- macro field_example(field) -%}
{%- if field.default is not none and field.default.kind == 'value' -%}{{ field.default.value_json }}
{%- elif field.array is not none -%}[{{ scalar_example(field.value) }}]
{%- else -%}{{ scalar_example(field.value) }}
{%- endif -%}
{%- endmacro -%}

{%- macro binding_example(operation, binding) -%}
{%- set state = namespace(value='"example"') -%}
{%- if binding.port.field is not none -%}
{%- for field in operation.input.fields -%}
{%- if field.name.raw == binding.port.field.raw -%}{%- set state.value = field_example(field) | trim -%}{%- endif -%}
{%- endfor -%}
{%- elif operation.input is not none and operation.input.kind == 'schema' -%}{%- set state.value='{}' -%}
{%- endif -%}
{{- state.value -}}
{%- endmacro -%}

{%- macro body_object(operation, bindings) -%}
{
{%- set first = namespace(value=true) -%}
{%- for binding in bindings -%}
{%- if binding.port.field is none -%}
{%- for field in operation.input.fields %}
  {% if not first.value %},{% endif %}{{ field.name.raw | tojson }}: {{ field_example(field) }}{% set first.value=false %}
{%- endfor -%}
{%- else -%}
{%- for field in operation.input.fields -%}
{%- if field.name.raw == binding.port.field.raw %}
  {% if not first.value %},{% endif %}{{ field.name.raw | tojson }}: {{ field_example(field) }}{% set first.value=false %}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- endfor %}
}
{%- endmacro -%}

export default function () {
{%- for operation in operations %}
{%- set httpfacet = operation.facets.http %}
  group({{ operation.name.title | tojson }}, function () {
{%- if httpfacet.input_groups.multipart %}
    console.warn({{ (operation.id ~ ': multipart skipped because Dryv has no first-class file-part semantics') | tojson }});
{%- else %}
    let path = {{ httpfacet.path.effective | tojson }};
{%- for binding in httpfacet.input_groups.path %}
    path = path.replace(
      '{' + {{ (binding.name or binding.port.name.raw) | tojson }} + '}',
      encodeURIComponent(String({{ binding_example(operation, binding) | trim }})),
    );
{%- endfor %}

    const query = [];
{%- for binding in httpfacet.input_groups.query %}
    {
      const value = {{ binding_example(operation, binding) | trim }};
      if (Array.isArray(value)) {
        for (const item of value) query.push([{{ (binding.name or binding.port.name.raw) | tojson }}, item]);
      } else if (value !== null && value !== undefined) {
        query.push([{{ (binding.name or binding.port.name.raw) | tojson }}, value]);
      }
    }
{%- endfor %}
{%- for binding in httpfacet.input_groups.query_object %}
{%- if binding.port.field is none %}
{%- for field in operation.input.fields %}
    {
      const value = {{ field_example(field) | trim }};
      if (Array.isArray(value)) {
        for (const item of value) query.push([{{ field.name.raw | tojson }}, item]);
      } else if (value !== null && value !== undefined) {
        query.push([{{ field.name.raw | tojson }}, value]);
      }
    }
{%- endfor %}
{%- else %}
    {
      const value = {{ binding_example(operation, binding) | trim }};
      if (value !== null && value !== undefined) {
        query.push([{{ (binding.name or binding.port.name.raw) | tojson }}, value]);
      }
    }
{%- endif %}
{%- endfor %}
    const queryString = query.length
      ? '?' + query.map(([name, value]) => encodeURIComponent(name) + '=' + encodeURIComponent(String(value))).join('&')
      : '';

    const headers = {};
{%- for binding in httpfacet.input_groups.header %}
    headers[{{ (binding.name or binding.port.name.raw) | tojson }}] = String({{ binding_example(operation, binding) | trim }});
{%- endfor %}
{%- if httpfacet.input_groups.cookie %}
    headers.Cookie = [{% for binding in httpfacet.input_groups.cookie %}{{ ((binding.name or binding.port.name.raw) ~ '=') | tojson }} + encodeURIComponent(String({{ binding_example(operation, binding) | trim }})){% if not loop.last %}, {% endif %}{% endfor %}].join('; ');
{%- endif %}

    let body = null;
{%- if httpfacet.input_groups.body %}
    body = JSON.stringify({{ body_object(operation, httpfacet.input_groups.body) | trim }});
    headers['Content-Type'] = 'application/json';
{%- elif httpfacet.input_groups.form %}
    const form = [];
{%- for binding in httpfacet.input_groups.form %}
{%- if binding.port.field is none %}
{%- for field in operation.input.fields %}
    {
      const value = {{ field_example(field) | trim }};
      if (Array.isArray(value)) {
        for (const item of value) form.push([{{ field.name.raw | tojson }}, item]);
      } else if (value !== null && value !== undefined) {
        form.push([{{ field.name.raw | tojson }}, value]);
      }
    }
{%- endfor %}
{%- else %}
    {
      const value = {{ binding_example(operation, binding) | trim }};
      if (Array.isArray(value)) {
        for (const item of value) form.push([{{ (binding.name or binding.port.name.raw) | tojson }}, item]);
      } else if (value !== null && value !== undefined) {
        form.push([{{ (binding.name or binding.port.name.raw) | tojson }}, value]);
      }
    }
{%- endif %}
{%- endfor %}
    body = form.map(([name, value]) => encodeURIComponent(name) + '=' + encodeURIComponent(String(value))).join('&');
    headers['Content-Type'] = 'application/x-www-form-urlencoded';
{%- endif %}

    const response = http.request(
      {{ httpfacet.method | tojson }},
      BASE_URL + path + queryString,
      body,
      { headers, tags: { dryv_operation: {{ operation.id | tojson }} } },
    );
    const expected = [{% if httpfacet.outputs %}{% for output in httpfacet.outputs %}{{ output.status }}{% if not loop.last %}, {% endif %}{% endfor %}{% else %}200{% endif %}];
    check(response, {
      [{{ (operation.name.title ~ ' returns declared success status') | tojson }}]: (res) => expected.includes(res.status),
    });
{%- endif %}
  });
{%- endfor %}
}