<commit_before>import { IMocker, IMockerOpts } from '@stoplight/prism-core/types';
import { IHttpOperation } from '@stoplight/types';
import { IHttpConfig, IHttpOperationConfig, IHttpRequest, IHttpResponse } from '../types';
import { IExampleGenerator } from './generator/IExampleGenerator';
import helpers from './negotiator/NegotiatorHelpers';

export class HttpMocker
  implements IMocker<IHttpOperation, IHttpRequest, IHttpConfig, IHttpResponse> {
  constructor(private _exampleGenerator: IExampleGenerator<any>) {}

  public async mock({
    resource,
    input,
    config,
  }: Partial<IMockerOpts<IHttpOperation, IHttpRequest, IHttpConfig>>): Promise<IHttpResponse> {
    // pre-requirements check
    if (!resource) {
      throw new Error('Resource is not defined');
    }

    if (!input) {
      throw new Error('Http request is not defined');
    }

    // setting default values
    config = config || { mock: true };

    const mockConfig: IHttpOperationConfig = typeof config.mock === 'boolean' ? {} : config.mock;

    // looking up proper example
    let negotiationResult;
    if (input.validations.input.length > 0) {
      negotiationResult = helpers.negotiateOptionsForInvalidRequest(resource.responses);
    } else {
      negotiationResult = helpers.negotiateOptionsForValidRequest(resource, mockConfig);
    }

    if (!negotiationResult.example && !negotiationResult.schema) {
      throw new Error('Neither example nor schema is defined');
    }

    // preparing response body
    const body =
      negotiationResult.example ||
      (await this._exampleGenerator.generate(
        negotiationResult.schema,
        negotiationResult.mediaType
      ));

    return {
      statusCode: parseInt(negotiationResult.code),
      headers: {
        'Content-type': negotiationResult.mediaType,
      },
      body,
    };
  }
}<commit_msg>Fixed mocked body for static example<commit_after>import { IMocker, IMockerOpts } from '@stoplight/prism-core/types';
import { IHttpOperation } from '@stoplight/types';
import { IHttpConfig, IHttpOperationConfig, IHttpRequest, IHttpResponse } from '../types';
import { IExampleGenerator } from './generator/IExampleGenerator';
import helpers from './negotiator/NegotiatorHelpers';

export class HttpMocker
  implements IMocker<IHttpOperation, IHttpRequest, IHttpConfig, IHttpResponse> {
  constructor(private _exampleGenerator: IExampleGenerator<any>) {}

  public async mock({
    resource,
    input,
    config,
  }: Partial<IMockerOpts<IHttpOperation, IHttpRequest, IHttpConfig>>): Promise<IHttpResponse> {
    // pre-requirements check
    if (!resource) {
      throw new Error('Resource is not defined');
    }

    if (!input) {
      throw new Error('Http request is not defined');
    }

    // setting default values
    config = config || { mock: true };

    const mockConfig: IHttpOperationConfig = typeof config.mock === 'boolean' ? {} : config.mock;

    // looking up proper example
    let negotiationResult;
    if (input.validations.input.length > 0) {
      negotiationResult = helpers.negotiateOptionsForInvalidRequest(resource.responses);
    } else {
      negotiationResult = helpers.negotiateOptionsForValidRequest(resource, mockConfig);
    }

    if (!negotiationResult.example && !negotiationResult.schema) {
      throw new Error('Neither example nor schema is defined');
    }

    // preparing response body
    let body;
    if (negotiationResult.example) {
      body = negotiationResult.example.value;
    } else {
      body = await this._exampleGenerator.generate(
        negotiationResult.schema,
        negotiationResult.mediaType
      );
    }

    return {
      statusCode: parseInt(negotiationResult.code),
      headers: {
        'Content-type': negotiationResult.mediaType,
      },
      body,
    };
  }
}
{'fraction_non_alphanumeric': 0.10602094240837696, 'fraction_numerical': 0.0005235602094240838, 'mean_word_length': 3.490011750881316, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2218502', 'n_tokens_mistral': 1082, 'n_tokens_neox': 1029, 'n_words': 252}