How to use the @foal/core.Context function in @foal/core

To help you get started, we’ve selected a few @foal/core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github FoalTS / foal / packages / swagger / src / swagger-controller.spec.ts View on Github external
it('should redirect the user to xxx/ if there is no trailing slash in the URL.', async () => {
      // This way, the browser requests the assets at the correct path (the relative path).
      const controller = new ConcreteClass();

      const ctx = new Context({ path: 'xxx' });
      const response = await controller.index(ctx);

      if (!isHttpResponseMovedPermanently(response)) {
        throw new Error('SwaggerController.index should return an HttpResponseMovedPermanently instance.');
      }

      strictEqual(response.path, ctx.request.path + '/');
    });
github FoalTS / foal / packages / cli / src / generate / specs / rest-api / test-foo-bar.controller.spec.ts View on Github external
it('should return an HttpResponseOK object with the testFooBar list.', async () => {
      const ctx = new Context({ query: {} });
      const response = await controller.get(ctx);

      if (!isHttpResponseOK(response)) {
        throw new Error('The returned value should be an HttpResponseOK object.');
      }

      if (!Array.isArray(response.body)) {
        throw new Error('The response body should be an array of testFooBars.');
      }

      strictEqual(response.body.length, 2);
      ok(response.body.find(testFooBar => testFooBar.text === testFooBar1.text));
      ok(response.body.find(testFooBar => testFooBar.text === testFooBar2.text));
    });
github FoalTS / foal / packages / cli / src / generate / specs / rest-api / test-foo-bar.controller.spec.ts View on Github external
it('should return an HttpResponseNotFound object if the testFooBar was not found.', async () => {
      const ctx = new Context({
        params: {
          testFooBarId: -1
        }
      });
      const response = await controller.getById(ctx);

      if (!isHttpResponseNotFound(response)) {
        throw new Error('The returned value should be an HttpResponseNotFound object.');
      }
    });
github FoalTS / foal / packages / cli / src / generate / specs / rest-api / test-foo-bar.controller.spec.ts View on Github external
+ 'an HttpResponseCreated object.', async () => {
      const ctx = new Context({
        body: {
          text: 'TestFooBar 3',
        }
      });
      const response = await controller.post(ctx);

      if (!isHttpResponseCreated(response)) {
        throw new Error('The returned value should be an HttpResponseCreated object.');
      }

      const testFooBar = await getRepository(TestFooBar).findOne({ text: 'TestFooBar 3' });

      if (!testFooBar) {
        throw new Error('No testFooBar 3 was found in the database.');
      }
github FoalTS / foal / packages / swagger / src / swagger-controller.spec.ts View on Github external
it('should return an HttpResponseNotFound if no controller is found with that name.', () => {
        class ConcreteClass extends SwaggerController {
          options = [
            { name: 'v1', url: 'http://example.com' },
          ];
        }
        const controller = new ConcreteClass();

        let ctx = new Context({ query: { name: 'v2' } });
        let response = controller.getOpenApiDefinition(ctx);
        if (!isHttpResponseNotFound(response)) {
          throw new Error('The response should be an instance of HttpResponseNotFound.');
        }

        ctx = new Context({ query: { name: 'v1' } });
        response = controller.getOpenApiDefinition(ctx);
        if (!isHttpResponseNotFound(response)) {
          throw new Error('The response should be an instance of HttpResponseNotFound.');
        }
      });
github FoalTS / foal / packages / examples / src / app / controllers / product.controller.spec.ts View on Github external
it('should return an HttpResponseNotFound if the object does not exist.', async () => {
      const ctx = new Context({
        body: {
          text: '',
        },
        params: {
          id: -1
        }
      });
      const response = await controller.putById(ctx);

      if (!isHttpResponseNotFound(response)) {
        throw new Error('The returned value should be an HttpResponseNotFound object.');
      }
    });
github FoalTS / foal / packages / jwt / src / jwt.hook.spec.ts View on Github external
it('should let ctx.user equal undefined if the cookie does not exist.', async () => {
          const hook = getHookFunction(JWT({ cookie: true }));

          const ctx = new Context({ get(str: string) { return undefined; }, cookies: {} });

          const response = await hook(ctx, services);
          strictEqual(response, undefined);
          strictEqual(ctx.user, undefined);
        });
github FoalTS / foal / packages / examples / src / app / controllers / product.controller.spec.ts View on Github external
it('should return an HttpResponseOK object with the product list.', async () => {
      const ctx = new Context({ query: {} });
      const response = await controller.get(ctx);

      if (!isHttpResponseOK(response)) {
        throw new Error('The returned value should be an HttpResponseOK object.');
      }

      if (!Array.isArray(response.body)) {
        throw new Error('The response body should be an array of products.');
      }

      strictEqual(response.body.length, 2);
      ok(response.body.find(product => product.text === product1.text));
      ok(response.body.find(product => product.text === product2.text));
    });
github FoalTS / foal / packages / password / src / validate-email-and-password-credentials-format.pre-hook.spec.ts View on Github external
it('should return an HttpResponseBadRequest if there are additional properties.', () => {
    const preHook = validateEmailAndPasswordCredentialsFormat();
    const ctx = new Context();
    ctx.request.body = {
      email: 'john@jack.com',
      foo: 'bar',
      password: 'myPassword',
    };

    const response = preHook(ctx, new ServiceManager());
    expect(response).to.be.instanceOf(HttpResponseBadRequest);
    expect((response as HttpResponseBadRequest).content).to.be.an('array').and.to.have.lengthOf(1);
    expect((response as HttpResponseBadRequest).content[0]).to.deep.equal({
      dataPath: '',
      keyword: 'additionalProperties',
      message: 'should NOT have additional properties',
      params: {
        additionalProperty: 'foo',
      },
github FoalTS / foal / packages / graphql / src / graphql.controller.spec.ts View on Github external
},
                    },
                    type: GraphQLString,
                    resolve(obj, args, context, info) {
                      return JSON.stringify(args);
                    }
                  }
                },
                name: 'RootQueryType',
              })
            });
          }
          controller = createController(ConcreteController);

          const query = `query a($input: String) { hello(input: $input) }`;
          const ctx = new Context({
            query: { query, variables: '{"input":"foobar"}' }
          });
          const response = await controller.post(ctx);

          if (!isHttpResponseOK(response)) {
            throw new Error('The controller should have returned an HttpResponseOK instance.');
          }

          deepStrictEqual(response.body, {
            data: {
              hello: '{"input":"foobar"}'
            }
          });
        });