VAPID

 view release on metacpan or  search on metacpan

t/01-test.t  view on Meta::CPAN

use Test::More;
use VAPID qw/all/;

ok(my ($pub, $priv) = generate_vapid_keys());

ok(validate_subject('mailto:thisusedtobeanemail@gmail.com'));
ok(validate_public_key($pub));
ok(validate_private_key($priv));
ok(validate_expiration(time + 60));

ok(my $header = generate_vapid_header(
	'https://fcm.googleapis.com',
	'mailto:thisusedtobeanemail@gmail.com',
	$pub,
	$priv,
	time + 60
));

# Test validate_subscription
my $valid_subscription = {
	endpoint => 'https://fcm.googleapis.com/fcm/send/test-endpoint',
	keys => {
		p256dh => $pub,
		auth => 'dGVzdGF1dGhrZXkxMjM0NQ'
	}
};

ok(validate_subscription($valid_subscription), 'validate_subscription with valid subscription');

# Test invalid subscriptions
eval { validate_subscription() };
ok($@ =~ /No subscription/, 'validate_subscription dies without subscription');

eval { validate_subscription('not a hash') };
ok($@ =~ /must be a hash/, 'validate_subscription dies with non-hash');

eval { validate_subscription({}) };
ok($@ =~ /must have an endpoint/, 'validate_subscription dies without endpoint');

eval { validate_subscription({ endpoint => 'https://example.com' }) };
ok($@ =~ /must have keys/, 'validate_subscription dies without keys');

eval { validate_subscription({ endpoint => 'https://example.com', keys => {} }) };
ok($@ =~ /must have a p256dh/, 'validate_subscription dies without p256dh');

eval { validate_subscription({ endpoint => 'https://example.com', keys => { p256dh => 'test' } }) };
ok($@ =~ /must have an auth/, 'validate_subscription dies without auth');

# Test encrypt_payload
my ($enc_pub, $enc_priv) = generate_vapid_keys();
my $test_subscription = {
	endpoint => 'https://fcm.googleapis.com/fcm/send/test',
	keys => {
		p256dh => $enc_pub,
		auth => 'dGVzdGF1dGhrZXkxMjM0NQ'
	}
};

ok(my $encrypted = encrypt_payload('Test message', $test_subscription), 'encrypt_payload works');
ok($encrypted->{ciphertext}, 'encrypt_payload returns ciphertext');
ok($encrypted->{salt}, 'encrypt_payload returns salt');
ok($encrypted->{local_public_key}, 'encrypt_payload returns local_public_key');

# Test encrypt_payload validation
eval { encrypt_payload() };
ok($@ =~ /No payload/, 'encrypt_payload dies without payload');

# Test build_push_request



( run in 1.693 second using v1.01-cache-2.11-cpan-d8267643d1d )