Best JavaScript code snippet using apickli
browser_JsonSchemaValidator.js
Source:browser_JsonSchemaValidator.js
1/* Any copyright is dedicated to the Public Domain.2 * http://creativecommons.org/publicdomain/zero/1.0/ */3"use strict";4ChromeUtils.import(5 "resource://gre/modules/components-utils/JsonSchemaValidator.jsm",6 this7);8add_task(async function test_boolean_values() {9 let schema = {10 type: "boolean",11 };12 let valid, parsed;13 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(14 true,15 schema16 );17 ok(valid && parsed === true, "Parsed boolean value correctly");18 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(19 false,20 schema21 );22 ok(valid && parsed === false, "Parsed boolean value correctly");23 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(0, schema);24 ok(valid && parsed === false, "0 parsed as false correctly");25 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(1, schema);26 ok(valid && parsed === true, "1 parsed as true correctly");27 // Invalid values:28 ok(29 !JsonSchemaValidator.validateAndParseParameters("0", schema)[0],30 "No type coercion"31 );32 ok(33 !JsonSchemaValidator.validateAndParseParameters("true", schema)[0],34 "No type coercion"35 );36 ok(37 !JsonSchemaValidator.validateAndParseParameters(2, schema)[0],38 "Other number values are not valid"39 );40 ok(41 !JsonSchemaValidator.validateAndParseParameters(undefined, schema)[0],42 "Invalid value"43 );44 ok(45 !JsonSchemaValidator.validateAndParseParameters({}, schema)[0],46 "Invalid value"47 );48 ok(49 !JsonSchemaValidator.validateAndParseParameters(null, schema)[0],50 "Invalid value"51 );52});53add_task(async function test_number_values() {54 let schema = {55 type: "number",56 };57 let valid, parsed;58 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(1, schema);59 ok(valid && parsed === 1, "Parsed number value correctly");60 // Invalid values:61 ok(62 !JsonSchemaValidator.validateAndParseParameters("1", schema)[0],63 "No type coercion"64 );65 ok(66 !JsonSchemaValidator.validateAndParseParameters(true, schema)[0],67 "Invalid value"68 );69 ok(70 !JsonSchemaValidator.validateAndParseParameters({}, schema)[0],71 "Invalid value"72 );73 ok(74 !JsonSchemaValidator.validateAndParseParameters(null, schema)[0],75 "Invalid value"76 );77});78add_task(async function test_integer_values() {79 // Integer is an alias for number80 let schema = {81 type: "integer",82 };83 let valid, parsed;84 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(1, schema);85 ok(valid && parsed == 1, "Parsed integer value correctly");86 // Invalid values:87 ok(88 !JsonSchemaValidator.validateAndParseParameters("1", schema)[0],89 "No type coercion"90 );91 ok(92 !JsonSchemaValidator.validateAndParseParameters(true, schema)[0],93 "Invalid value"94 );95 ok(96 !JsonSchemaValidator.validateAndParseParameters({}, schema)[0],97 "Invalid value"98 );99 ok(100 !JsonSchemaValidator.validateAndParseParameters(null, schema)[0],101 "Invalid value"102 );103});104add_task(async function test_null_values() {105 let schema = {106 type: "null",107 };108 let valid, parsed;109 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(110 null,111 schema112 );113 ok(valid, "Null should be valid");114 ok(parsed === null, "Parsed value should be null");115 // Invalid values:116 ok(117 !JsonSchemaValidator.validateAndParseParameters(1, schema)[0],118 "Number should be invalid"119 );120 ok(121 !JsonSchemaValidator.validateAndParseParameters("1", schema)[0],122 "String should be invalid"123 );124 ok(125 !JsonSchemaValidator.validateAndParseParameters(true, schema)[0],126 "Boolean should be invalid"127 );128 ok(129 !JsonSchemaValidator.validateAndParseParameters({}, schema)[0],130 "Object should be invalid"131 );132 ok(133 !JsonSchemaValidator.validateAndParseParameters([], schema)[0],134 "Array should be invalid"135 );136});137add_task(async function test_string_values() {138 let schema = {139 type: "string",140 };141 let valid, parsed;142 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(143 "foobar",144 schema145 );146 ok(valid && parsed == "foobar", "Parsed string value correctly");147 // Invalid values:148 ok(149 !JsonSchemaValidator.validateAndParseParameters(1, schema)[0],150 "No type coercion"151 );152 ok(153 !JsonSchemaValidator.validateAndParseParameters(true, schema)[0],154 "No type coercion"155 );156 ok(157 !JsonSchemaValidator.validateAndParseParameters(undefined, schema)[0],158 "Invalid value"159 );160 ok(161 !JsonSchemaValidator.validateAndParseParameters({}, schema)[0],162 "Invalid value"163 );164 ok(165 !JsonSchemaValidator.validateAndParseParameters(null, schema)[0],166 "Invalid value"167 );168});169add_task(async function test_URL_values() {170 let schema = {171 type: "URL",172 };173 let valid, parsed;174 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(175 "https://www.example.com/foo#bar",176 schema177 );178 ok(valid, "URL is valid");179 ok(parsed instanceof URL, "parsed is a URL");180 is(parsed.origin, "https://www.example.com", "origin is correct");181 is(parsed.pathname + parsed.hash, "/foo#bar", "pathname is correct");182 // Invalid values:183 ok(184 !JsonSchemaValidator.validateAndParseParameters("", schema)[0],185 "Empty string is not accepted for URL"186 );187 ok(188 !JsonSchemaValidator.validateAndParseParameters(189 "www.example.com",190 schema191 )[0],192 "Scheme is required for URL"193 );194 ok(195 !JsonSchemaValidator.validateAndParseParameters("https://:!$%", schema)[0],196 "Invalid URL"197 );198 ok(199 !JsonSchemaValidator.validateAndParseParameters({}, schema)[0],200 "Invalid value"201 );202});203add_task(async function test_URLorEmpty_values() {204 let schema = {205 type: "URLorEmpty",206 };207 let valid, parsed;208 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(209 "https://www.example.com/foo#bar",210 schema211 );212 ok(valid, "URL is valid");213 ok(parsed instanceof URL, "parsed is a nsIURI");214 is(parsed.origin, "https://www.example.com", "origin is correct");215 is(parsed.pathname + parsed.hash, "/foo#bar", "pathname is correct");216 // Test that this type also accept empty strings217 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters("", schema);218 ok(valid, "URLorEmpty is valid");219 ok(!parsed, "parsed value is falsy");220 is(typeof parsed, "string", "parsed is a string");221 is(parsed, "", "parsed is an empty string");222 // Invalid values:223 ok(224 !JsonSchemaValidator.validateAndParseParameters(" ", schema)[0],225 "Non-empty string is not accepted"226 );227 ok(228 !JsonSchemaValidator.validateAndParseParameters(229 "www.example.com",230 schema231 )[0],232 "Scheme is required for URL"233 );234 ok(235 !JsonSchemaValidator.validateAndParseParameters("https://:!$%", schema)[0],236 "Invalid URL"237 );238 ok(239 !JsonSchemaValidator.validateAndParseParameters({}, schema)[0],240 "Invalid value"241 );242});243add_task(async function test_origin_values() {244 // Origin is a URL that doesn't contain a path/query string (i.e., it's only scheme + host + port)245 let schema = {246 type: "origin",247 };248 let valid, parsed;249 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(250 "https://www.example.com",251 schema252 );253 ok(valid, "Origin is valid");254 ok(parsed instanceof URL, "parsed is a nsIURI");255 is(parsed.origin, "https://www.example.com", "origin is correct");256 is(parsed.pathname + parsed.hash, "/", "pathname is corect");257 // Invalid values:258 ok(259 !JsonSchemaValidator.validateAndParseParameters(260 "https://www.example.com/foobar",261 schema262 )[0],263 "Origin cannot contain a path part"264 );265 ok(266 !JsonSchemaValidator.validateAndParseParameters("https://:!$%", schema)[0],267 "Invalid origin"268 );269 ok(270 !JsonSchemaValidator.validateAndParseParameters({}, schema)[0],271 "Invalid value"272 );273});274add_task(async function test_origin_file_values() {275 // File URLs can also be origins276 let schema = {277 type: "origin",278 };279 let valid, parsed;280 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(281 "file:///foo/bar",282 schema283 );284 ok(valid, "Origin is valid");285 ok(parsed instanceof URL, "parsed is a nsIURI");286 is(parsed.href, "file:///foo/bar", "Should get what we passed in");287});288add_task(async function test_origin_file_values() {289 // File URLs can also be origins290 let schema = {291 type: "origin",292 };293 let valid, parsed;294 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(295 "file:///foo/bar/foobar.html",296 schema297 );298 ok(valid, "Origin is valid");299 ok(parsed instanceof URL, "parsed is a nsIURI");300 is(301 parsed.href,302 "file:///foo/bar/foobar.html",303 "Should get what we passed in"304 );305});306add_task(async function test_array_values() {307 // The types inside an array object must all be the same308 let schema = {309 type: "array",310 items: {311 type: "number",312 },313 };314 let valid, parsed;315 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(316 [1, 2, 3],317 schema318 );319 ok(valid, "Array is valid");320 ok(Array.isArray(parsed), "parsed is an array");321 is(parsed.length, 3, "array is correct");322 // An empty array is also valid323 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters([], schema);324 ok(valid, "Array is valid");325 ok(Array.isArray(parsed), "parsed is an array");326 is(parsed.length, 0, "array is correct");327 // Invalid values:328 ok(329 !JsonSchemaValidator.validateAndParseParameters([1, true, 3], schema)[0],330 "Mixed types"331 );332 ok(333 !JsonSchemaValidator.validateAndParseParameters(2, schema)[0],334 "Type is correct but not in an array"335 );336 ok(337 !JsonSchemaValidator.validateAndParseParameters({}, schema)[0],338 "Object is not an array"339 );340});341add_task(async function test_non_strict_arrays() {342 // Non-strict arrays ignores invalid values (don't include343 // them in the parsed output), instead of failing the validation.344 // Note: invalid values might still report errors to the console.345 let schema = {346 type: "array",347 strict: false,348 items: {349 type: "string",350 },351 };352 let valid, parsed;353 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(354 ["valid1", "valid2", false, 3, "valid3"],355 schema356 );357 ok(valid, "Array is valid");358 ok(Array.isArray(parsed, "parsed is an array"));359 is(parsed.length, 3, "Only valid values were included in the parsed array");360 Assert.deepEqual(361 parsed,362 ["valid1", "valid2", "valid3"],363 "Results were expected"364 );365 // Checks that strict defaults to true;366 delete schema.strict;367 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(368 ["valid1", "valid2", false, 3, "valid3"],369 schema370 );371 ok(!valid, "Same verification was invalid without strict=false");372});373add_task(async function test_object_values() {374 let schema = {375 type: "object",376 properties: {377 url: {378 type: "URL",379 },380 title: {381 type: "string",382 },383 },384 };385 let valid, parsed;386 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(387 {388 url: "https://www.example.com/foo#bar",389 title: "Foo",390 alias: "Bar",391 },392 schema393 );394 ok(valid, "Object is valid");395 ok(typeof parsed == "object", "parsed in an object");396 ok(parsed.url instanceof URL, "types inside the object are also parsed");397 is(398 parsed.url.href,399 "https://www.example.com/foo#bar",400 "URL was correctly parsed"401 );402 is(parsed.title, "Foo", "title was correctly parsed");403 is(404 parsed.alias,405 undefined,406 "property not described in the schema is not present in the parsed object"407 );408 // Invalid values:409 ok(410 !JsonSchemaValidator.validateAndParseParameters(411 {412 url: "https://www.example.com/foo#bar",413 title: 3,414 },415 schema416 )[0],417 "Mismatched type for title"418 );419 ok(420 !JsonSchemaValidator.validateAndParseParameters(421 {422 url: "www.example.com",423 title: 3,424 },425 schema426 )[0],427 "Invalid URL inside the object"428 );429});430add_task(async function test_array_of_objects() {431 // This schema is used, for example, for bookmarks432 let schema = {433 type: "array",434 items: {435 type: "object",436 properties: {437 url: {438 type: "URL",439 },440 title: {441 type: "string",442 },443 },444 },445 };446 let valid, parsed;447 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(448 [449 {450 url: "https://www.example.com/bookmark1",451 title: "Foo",452 },453 {454 url: "https://www.example.com/bookmark2",455 title: "Bar",456 },457 ],458 schema459 );460 ok(valid, "Array is valid");461 is(parsed.length, 2, "Correct number of items");462 ok(463 typeof parsed[0] == "object" && typeof parsed[1] == "object",464 "Correct objects inside array"465 );466 is(467 parsed[0].url.href,468 "https://www.example.com/bookmark1",469 "Correct URL for bookmark 1"470 );471 is(472 parsed[1].url.href,473 "https://www.example.com/bookmark2",474 "Correct URL for bookmark 2"475 );476 is(parsed[0].title, "Foo", "Correct title for bookmark 1");477 is(parsed[1].title, "Bar", "Correct title for bookmark 2");478});479add_task(async function test_missing_arrays_inside_objects() {480 let schema = {481 type: "object",482 properties: {483 allow: {484 type: "array",485 items: {486 type: "boolean",487 },488 },489 block: {490 type: "array",491 items: {492 type: "boolean",493 },494 },495 },496 };497 let valid, parsed;498 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(499 {500 allow: [true, true, true],501 },502 schema503 );504 ok(valid, "Object is valid");505 is(parsed.allow.length, 3, "Allow array is correct.");506 is(parsed.block, undefined, "Block array is undefined, as expected.");507});508add_task(async function test_required_vs_nonrequired_properties() {509 let schema = {510 type: "object",511 properties: {512 "non-required-property": {513 type: "number",514 },515 "required-property": {516 type: "number",517 },518 },519 required: ["required-property"],520 };521 let valid, parsed;522 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(523 {524 "required-property": 5,525 },526 schema527 );528 ok(valid, "Object is valid since required property is present");529 is(parsed["required-property"], 5, "required property is correct");530 is(531 parsed["non-required-property"],532 undefined,533 "non-required property is undefined, as expected"534 );535 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(536 {537 "non-required-property": 5,538 },539 schema540 );541 ok(!valid, "Object is not valid since the required property is missing");542 is(parsed, null, "Nothing was returned as parsed");543});544add_task(async function test_number_or_string_values() {545 let schema = {546 type: ["number", "string"],547 };548 let valid, parsed;549 // valid values550 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(1, schema);551 ok(valid && parsed === 1, "Parsed number value correctly");552 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(553 "foobar",554 schema555 );556 ok(valid && parsed === "foobar", "Parsed string value correctly");557 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters("1", schema);558 ok(valid && parsed === "1", "Did not coerce string to number");559 // Invalid values:560 ok(561 !JsonSchemaValidator.validateAndParseParameters(true, schema)[0],562 "Invalid value"563 );564 ok(565 !JsonSchemaValidator.validateAndParseParameters({}, schema)[0],566 "Invalid value"567 );568 ok(569 !JsonSchemaValidator.validateAndParseParameters(null, schema)[0],570 "Invalid value"571 );572});573add_task(async function test_number_or_array_values() {574 let schema = {575 type: ["number", "array"],576 items: {577 type: "number",578 },579 };580 let valid, parsed;581 // valid values582 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(1, schema);583 ok(valid, "Number is valid");584 is(parsed, 1, "Parsed correctly");585 ok(valid && parsed === 1, "Parsed number value correctly");586 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(587 [1, 2, 3],588 schema589 );590 ok(valid, "Array is valid");591 Assert.deepEqual(parsed, [1, 2, 3], "Parsed correctly");592 // Invalid values:593 ok(594 !JsonSchemaValidator.validateAndParseParameters(true, schema)[0],595 "Invalid value"596 );597 ok(598 !JsonSchemaValidator.validateAndParseParameters({}, schema)[0],599 "Invalid value"600 );601 ok(602 !JsonSchemaValidator.validateAndParseParameters(null, schema)[0],603 "Invalid value"604 );605 ok(606 !JsonSchemaValidator.validateAndParseParameters(["a", "b"], schema)[0],607 "Invalid value"608 );609 ok(610 !JsonSchemaValidator.validateAndParseParameters([[]], schema)[0],611 "Invalid value"612 );613 ok(614 !JsonSchemaValidator.validateAndParseParameters([0, 1, [2, 3]], schema)[0],615 "Invalid value"616 );617});618add_task(function test_number_or_null_Values() {619 let schema = {620 type: ["number", "null"],621 };622 let valid, parsed;623 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(1, schema);624 ok(valid, "Number should be valid");625 is(parsed, 1, "Number should be parsed correctly");626 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(627 null,628 schema629 );630 ok(valid, "Null should be valid");631 is(parsed, null, "Null should be parsed correctly");632 // Invalid values:633 ok(634 !JsonSchemaValidator.validateAndParseParameters(true, schema)[0],635 "Boolean should be rejected"636 );637 ok(638 !JsonSchemaValidator.validateAndParseParameters("string", schema)[0],639 "String should be rejected"640 );641 ok(642 !JsonSchemaValidator.validateAndParseParameters({}, schema)[0],643 "Object should be rejected"644 );645 ok(646 !JsonSchemaValidator.validateAndParseParameters(["a", "b"], schema)[0],647 "Array should be rejected"648 );649});650add_task(async function test_patternProperties() {651 let schema = {652 type: "object",653 properties: {654 "S-bool-property": { type: "boolean" },655 },656 patternProperties: {657 "^S-": { type: "string" },658 "^N-": { type: "number" },659 "^B-": { type: "boolean" },660 },661 };662 let valid, parsed;663 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(664 {665 "S-string": "test",666 "N-number": 5,667 "B-boolean": true,668 "S-bool-property": false,669 },670 schema671 );672 ok(valid, "Object is valid");673 is(parsed["S-string"], "test", "parsedProperty is correct");674 is(parsed["N-number"], 5, "parsedProperty is correct");675 is(parsed["B-boolean"], true, "parsedProperty is correct");676 is(parsed["S-bool-property"], false, "property is correct");677 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(678 {679 "N-string": "test",680 },681 schema682 );683 ok(!valid, "Object is not valid since there is a type mismatch");684 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(685 {686 "S-number": 5,687 },688 schema689 );690 ok(!valid, "Object is not valid since there is a type mismatch");691 schema = {692 type: "object",693 patternProperties: {694 "[": { " type": "string" },695 },696 };697 Assert.throws(698 () => {699 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(700 {},701 schema702 );703 },704 /Invalid property pattern/,705 "Checking that invalid property patterns throw"706 );707});708add_task(async function test_JSON_type() {709 let schema = {710 type: "JSON",711 };712 let valid, parsed;713 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(714 {715 a: "b",716 },717 schema718 );719 ok(valid, "Object is valid");720 ok(typeof parsed == "object", "parsed in an object");721 is(parsed.a, "b", "parsedProperty is correct");722 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(723 '{"a": "b"}',724 schema725 );726 ok(valid, "Object is valid");727 ok(typeof parsed == "object", "parsed in an object");728 is(parsed.a, "b", "parsedProperty is correct");729 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(730 "{This{is{not{JSON}}}}",731 schema732 );733 ok(!valid, "Object is not valid since JSON was incorrect");734 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters("0", schema);735 ok(!valid, "Object is not valid since input wasn't an object");736 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(737 "true",738 schema739 );740 ok(!valid, "Object is not valid since input wasn't an object");741});742add_task(async function test_enum() {743 let schema = {744 type: "string",745 enum: ["one", "two"],746 };747 let valid, parsed;748 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(749 "one",750 schema751 );752 ok(valid && parsed == "one", "Parsed string value correctly");753 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(754 "three",755 schema756 );757 ok(!valid, "String value wasn't in enum");758});759add_task(async function test_bool_enum() {760 let schema = {761 type: "boolean",762 enum: ["one", "two"],763 };764 let valid, parsed;765 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(766 true,767 schema768 );769 ok(valid && parsed === true, "Enum is ignored because it is a boolean.");770});771add_task(async function test_boolint_enum() {772 let schema = {773 type: "boolean",774 enum: ["one", "two"],775 };776 let valid, parsed;777 [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(1, schema);778 ok(779 valid && parsed === true,780 "Enum is ignored because it is a boolean converted from an integer."781 );...
Using AI Code Generation
1var apickli = require('apickli');2var jsonSchemaValidator = require('apickli-json-schema-validator');3var {defineSupportCode} = require('cucumber');4var {setWorldConstructor} = require('cucumber');5function CustomWorld() {6 apickli.Apickli.call(this, 'http', 'localhost:8080');7 this.jsonSchemaValidator = jsonSchemaValidator;8}9setWorldConstructor(CustomWorld);10defineSupportCode(function ({Before, Given, When, Then}) {11 Before(function (scenario, callback) {12 this.apickli.resetRequest();13 callback();14 });15 Given(/^I set request header "([^"]*)" to "([^"]*)"$/, function (header, value, callback) {16 this.apickli.addRequestHeader(header, value);17 callback();18 });19 When(/^I GET "([^"]*)"$/, function (resource, callback) {20 this.apickli.get(resource, callback);21 });22 Then(/^I should get JSON response with "([^"]*)"$/, function (response, callback) {23 this.jsonSchemaValidator.validateResponse(this.apickli, response, callback);24 });25});26var apickli = require('apickli');27var jsonSchemaValidator = require('apickli-json-schema-validator');28var {defineSupportCode} = require('cucumber');29var {setWorldConstructor} = require('cucumber');30function CustomWorld() {31 apickli.Apickli.call(this, 'http', 'localhost:8080');32 this.jsonSchemaValidator = jsonSchemaValidator;33}34setWorldConstructor(CustomWorld);35defineSupportCode(function ({Before, Given, When, Then}) {36 Before(function (scenario, callback) {37 this.apickli.resetRequest();38 callback();39 });40 Given(/^I set request header "([^"]*)" to "([^"]*)"$/, function (header, value, callback) {41 this.apickli.addRequestHeader(header, value);42 callback();43 });44 When(/^I GET "([^"]*)"$/, function (resource, callback) {45 this.apickli.get(resource, callback);46 });47 Then(/^I should get JSON response with "([^"]*)"$/, function (response, callback) {48 this.jsonSchemaValidator.validateResponse(this.apickli, response, callback);
Using AI Code Generation
1const { defineSupportCode } = require('cucumber');2const Apickli = require('apickli');3const { setDefaultTimeout } = require('cucumber');4defineSupportCode(function({ Before, setDefaultTimeout }) {5 Before(function() {6 this.apickli = new Apickli.Apickli('http', 'localhost:8080');7 this.apickli.addRequestHeader('Content-Type', 'application/json');8 });9 setDefaultTimeout(60 * 1000);10});11const { defineSupportCode } = require('cucumber');12const Apickli = require('apickli');13const { setDefaultTimeout } = require('cucumber');14defineSupportCode(function({ Before, setDefaultTimeout }) {15 Before(function() {16 this.apickli = new Apickli.Apickli('http', 'localhost:8080');17 this.apickli.addRequestHeader('Content-Type', 'application/json');18 });19 setDefaultTimeout(60 * 1000);20});21const { defineSupportCode } = require('cucumber');22const Apickli = require('apickli');23const { setDefaultTimeout } = require('cucumber');24defineSupportCode(function({ Before, setDefaultTimeout }) {25 Before(function() {26 this.apickli = new Apickli.Apickli('http', 'localhost:8080');27 this.apickli.addRequestHeader('Content-Type', 'application/json');28 });29 setDefaultTimeout(60 * 1000);30});31const { defineSupportCode } = require('cucumber');32const Apickli = require('apickli');33const { setDefaultTimeout } = require('cucumber');34defineSupportCode(function({ Before, setDefaultTimeout }) {35 Before(function() {36 this.apickli = new Apickli.Apickli('http', 'localhost:8080');37 this.apickli.addRequestHeader('Content-Type', 'application/json');38 });39 setDefaultTimeout(60 * 1000);40});41const { defineSupportCode } = require('cucumber');42const Apickli = require('apickli');43const { setDefaultTimeout } = require('cucumber
Using AI Code Generation
1var apickli = require('apickli');2var jsonSchemaValidator = require('json-schema-validator');3var chai = require('chai');4var expect = chai.expect;5module.exports = function () {6 this.Given(/^I validate the response against the schema$/, function (callback) {7 jsonSchemaValidator.validate(this.apickli.getResponseObject(), this.apickli.getSchemaObject(), function (err) {8 if (err) {9 console.log(err);10 expect(err).to.equal(null);11 }12 callback();13 });14 });15};16var apickli = require('apickli');17var chai = require('chai');18var expect = chai.expect;19var testSteps = function () {20 this.Before(function (callback) {21 this.apickli = new apickli.Apickli('http', 'localhost:8080');22 this.apickli.addRequestHeader('Content-Type', 'application/json');23 this.apickli.addRequestHeader('Accept', 'application/json');24 callback();25 });26 this.Given(/^I set the schema file to (.*)$/, function (schemaFile, callback) {27 this.apickli.storeValueInScenarioScope('schemaFile', schemaFile);28 this.apickli.storeValueInScenarioScope('schema', require('../schemas/' + schemaFile));29 callback();30 });31 this.Given(/^I set the request body to (.*)$/, function (jsonString, callback) {32 this.apickli.storeValueInScenarioScope('requestBody', jsonString);33 callback();34 });35 this.When(/^I POST to (.*)$/, function (path, callback) {36 this.apickli.storeValueInScenarioScope('path', path);37 this.apickli.post(path, this.apickli.getVariableValue('requestBody'), callback);38 });39 this.Then(/^I should get a (\d+) response$/, function (statusCode, callback) {40 expect(this.apickli.getResponseObject().statusCode).to.equal(statusCode);41 callback();42 });43 this.Then(/^I validate the response against the schema$/, function (callback) {44 jsonSchemaValidator.validate(this.apickli.getResponseObject(), this.apickli.getSchemaObject(), function (err) {45 if (err) {46 console.log(err);47 expect(err).to.equal(null);48 }49 callback();50 });51 });52};
Using AI Code Generation
1var apickli = require('apickli');2var {defineSupportCode} = require('cucumber');3defineSupportCode(function({Given, When, Then}) {4 Given('I set json schema to {stringInDoubleQuotes}', function(jsonSchema, callback) {5 this.apickli.storeValueInScenarioScope('jsonSchema', jsonSchema);6 callback();7 });8 Then('I validate response body with json schema', function(callback) {9 var jsonSchema = this.apickli.getVariableValue('jsonSchema');10 var schema = JSON.parse(jsonSchema);11 this.apickli.jsonSchemaValidator(schema);12 callback();13 });14});15var apickli = require('apickli');16var {defineSupportCode} = require('cucumber');17defineSupportCode(function({Given, When, Then}) {18 Given('I set json schema to {stringInDoubleQuotes}', function(jsonSchema, callback) {19 this.apickli.storeValueInScenarioScope('jsonSchema', jsonSchema);20 callback();21 });22 Then('I validate response body with json schema', function(callback) {23 var jsonSchema = this.apickli.getVariableValue('jsonSchema');24 var schema = JSON.parse(jsonSchema);25 this.apickli.jsonSchemaValidator(schema);26 callback();27 });28});29Given I set json schema to "{'type':'object','properties':{'id':{'type':'number'},'name':{'type':'string'}}}"30{31}
Using AI Code Generation
1const { Before } = require('cucumber');2const Apickli = require('apickli');3const {setWorldConstructor} = require('cucumber');4const jsonSchemaValidator = require('apickli-json-schema-validator');5const apickli = new Apickli('http', 'localhost:8000');6apickli.addPlugin(jsonSchemaValidator);7apickli.storeValueInScenarioScope('apickli', apickli);8Before(function () {9 this.apickli = apickli;10});11const {defineSupportCode} = require('cucumber');12defineSupportCode(function({Given, When, Then}) {13 Given('I set header {string} to {string}', function (header, value) {14 this.apickli.addRequestHeader(header, value);15 });16 When('I GET {string}', function (url, callback) {17 this.apickli.get(url, callback);18 });19 Then('I should get JSON response', function (callback) {20 this.apickli.assertResponseContainsJson();21 callback();22 });23 Then('I should get JSON response matching schema {string}', function (schema, callback) {24 this.apickli.assertResponseMatchesJsonSchema(schema);25 callback();26 });27});28const {defineSupportCode} = require('cucumber');29defineSupportCode(function({Given, When, Then}) {30 Given('I set header {string} to {string}', function (header, value) {31 this.apickli.addRequestHeader(header, value);32 });33 When('I GET {string}', function (url, callback) {34 this.apickli.get(url, callback);35 });36 Then('I should get JSON response', function (callback) {37 this.apickli.assertResponseContainsJson();38 callback();39 });40 Then('I should get JSON response matching schema {string}', function (schema, callback) {41 this.apickli.assertResponseMatchesJsonSchema(schema);42 callback();43 });44});45const {defineSupportCode} = require('cucumber');46defineSupportCode(function({Given, When, Then}) {47 Given('I set header {string} to {string}', function (header, value) {48 this.apickli.addRequestHeader(header, value
Using AI Code Generation
1var jsonSchemaValidator = require('apickli/apickli-gherkin').jsonSchemaValidator;2var JsonSchemaValidator = new jsonSchemaValidator();3var myJsonSchema = {4 "properties": {5 "id": {6 },7 "name": {8 },9 "email": {10 }11 },12};13JsonSchemaValidator.validateJsonSchema(myJsonSchema, this.getResponseObject().body);
Using AI Code Generation
1const apickli = require('apickli');2const { Before, setWorldConstructor } = require('cucumber');3const jsonSchemaValidator = require('apickli-json-schema-validator');4const apickliJsonSchemaValidator = new jsonSchemaValidator(apickli);5Before(function () {6 this.apickli = new apickli.Apickli('http', 'localhost:8080');7 this.apickli.addPlugin(apickliJsonSchemaValidator);8});9const { Given, When, Then } = require('cucumber');10Given('I set header {string} to {string}', function (header, value) {11 this.apickli.addRequestHeader(header, value);12});13When('I GET {string}', function (resource, callback) {14 this.apickli.get(resource, callback);15});16Then('I validate response against schema {string}', function (schema, callback) {17 this.apickli.validateResponseAgainstJsonSchema(schema, callback);18});19When('I POST {string}', function (resource, callback) {20 this.apickli.post(resource, callback);21});22When('I PUT {string}', function (resource, callback) {23 this.apickli.put(resource, callback);24});25When('I DELETE {string}', function (resource, callback) {26 this.apickli.delete(resource, callback);27});28Then('I validate response code is {int}', function (status, callback) {29 this.apickli.assertResponseCode(status);30 callback();31});32Then('I validate response contains {string}', function (text, callback) {33 this.apickli.assertContainsText(text);34 callback();35});36Then('I validate response does not contain {string}', function (text, callback) {37 this.apickli.assertNotContainsText(text);38 callback();39});40Then('I validate response contains header {string} with value {string}', function (header, value, callback) {41 this.apickli.assertResponseContainsHeader(header, value);42 callback();43});44Then('I validate response does not contain header {string}', function (header, callback) {45 this.apickli.assertResponseDoesNotContainHeader(header);46 callback();47});48Then('I validate response header {string} matches {string}', function (header, regexp, callback) {49 this.apickli.assertResponseHeaderMatchesExpression(header, regexp);50 callback();51});52Then('I validate response header {string} does
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!