import { ExecutionContext, UnsupportedMediaTypeException } from '@nestjs/common';
import { MultipartFormDataGuard } from './multipart-form-data.guard';

describe('MultipartFormDataGuard', () => {
  const guard = new MultipartFormDataGuard();

  const createContext = (contentType: string | null): ExecutionContext =>
    ({
      switchToHttp: () => ({
        getRequest: () => ({
          is: (type: string) => (contentType === type ? contentType : false),
        }),
      }),
    }) as ExecutionContext;

  it('allows multipart/form-data requests', () => {
    expect(guard.canActivate(createContext('multipart/form-data'))).toBe(true);
  });

  it('rejects non-multipart requests', () => {
    expect(() => guard.canActivate(createContext('application/json'))).toThrow(
      new UnsupportedMediaTypeException('Content-Type must be multipart/form-data'),
    );
  });

  it('rejects requests without a content type match', () => {
    expect(() => guard.canActivate(createContext(null))).toThrow(UnsupportedMediaTypeException);
  });
});
