Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

Fix failing test and move new test

Ondra Urban142a43bed5c3
diff --git a/src/key_value_store.js b/src/key_value_store.js
index 2ae5ce433..15f17623d 100644
--- a/src/key_value_store.js
+++ b/src/key_value_store.js
@@ -56,13 +56,13 @@ const validateSetValueParams = (key, value, options) => {
checkParamOrThrow(value, 'value', 'Buffer | String', 'The "value" parameter must be a String or Buffer when "options.contentType" is specified.'); // eslint-disable-line max-len
}
+ if (options.contentType === '') throw new Error('Parameter options.contentType cannot be empty string.');
+ if (!key) throw new Error('The "key" parameter cannot be empty');
+
if (!KEY_VALUE_STORE_KEY_REGEX.test(key)) {
throw new Error('The "key" parameter may contain only the following characters: ' +
"[a-zA-Z0-9!-_.'()");
}
-
- if (options.contentType === '') throw new Error('Parameter options.contentType cannot be empty string.');
- if (!key) throw new Error('The "key" parameter cannot be empty');
};
/**
diff --git a/test/key_value_store.js b/test/key_value_store.js
index 008c5874c..fd3e823cd 100644
--- a/test/key_value_store.js
+++ b/test/key_value_store.js
@@ -73,22 +73,6 @@ describe('KeyValueStore', () => {
await store.delete();
expectDirEmpty(storeDir);
});
-
- it('should throw on invalid keys', async () => {
- const store = new KeyValueStoreLocal('my-store-id', LOCAL_EMULATION_DIR);
- const INVALID_CHARACTERS = '?|\\/"*<>%:';
- let counter = 0;
-
- for (const char of INVALID_CHARACTERS) { // eslint-disable-line
- try {
- await store.setValue(`my_id_${char}`);
- } catch (e) {
- counter++;
- }
- }
-
- expect(counter).to.be.eql(INVALID_CHARACTERS.length);
- });
});
describe('remote', async () => {
@@ -287,6 +271,22 @@ describe('KeyValueStore', () => {
.to.be.rejectedWith('Parameter options.contentType cannot be empty string.');
});
+ it('throws on invalid characters in key', async () => {
+ const store = new KeyValueStoreLocal('my-store-id', LOCAL_EMULATION_DIR);
+ const INVALID_CHARACTERS = '?|\\/"*<>%:';
+ let counter = 0;
+
+ for (const char of INVALID_CHARACTERS) { // eslint-disable-line
+ try {
+ await store.setValue(`my_id_${char}`);
+ } catch (e) {
+ counter++;
+ }
+ }
+
+ expect(counter).to.be.eql(INVALID_CHARACTERS.length);
+ });
+
it('throws if APIFY_DEFAULT_KEY_VALUE_STORE_ID env var is not defined', async () => {
const errMsg = 'The \'APIFY_DEFAULT_KEY_VALUE_STORE_ID\' environment variable is not defined';