1 | var util = require('../lib/diameter-util');
|
2 | var _ = require('lodash');
|
3 |
|
4 | describe('diameter-util', function() {
|
5 |
|
6 | it('generates random number', function() {
|
7 | expect(util.random32BitNumber()).toBeGreaterThan(0);
|
8 | });
|
9 |
|
10 | it('gets AVP value from a diameter message', function() {
|
11 | this.addMatchers({
|
12 | toDeepEqual: function(expected) {
|
13 | return _.isEqual(this.actual, expected);
|
14 | }
|
15 | });
|
16 |
|
17 | var exampleMessage = [
|
18 | ['Result-Code', 2001],
|
19 | [264, 'test.com'],
|
20 | ['Origin-Realm', 'com'],
|
21 | ['Auth-Application-Id', 'Diameter Credit Control'],
|
22 | ['CC-Request-Type', 'INITIAL_REQUEST'],
|
23 | ['CC-Request-Number', 0],
|
24 | ['Multiple-Services-Credit-Control', [
|
25 | ['Granted-Service-Unit', [
|
26 | ['CC-Time', 123],
|
27 | ['CC-Money', [
|
28 | ['Unit-Value', [
|
29 | ['Value-Digits', 123],
|
30 | ['Exponent', 1]
|
31 | ]],
|
32 | ['Currency-Code', 1]
|
33 | ]],
|
34 | ['CC-Total-Octets', 123],
|
35 | ['CC-Input-Octets', 123],
|
36 | ['CC-Output-Octets', 123]
|
37 | ]],
|
38 | ['Requested-Service-Unit', [
|
39 | ['CC-Time', 123],
|
40 | ['CC-Money', [
|
41 | ['Unit-Value', [
|
42 | ['Value-Digits', 123],
|
43 | ['Exponent', 1]
|
44 | ]],
|
45 | ['Currency-Code', 1]
|
46 | ]],
|
47 | ['CC-Total-Octets', 123],
|
48 | ['CC-Input-Octets', 123],
|
49 | ['CC-Output-Octets', 123]
|
50 | ]],
|
51 | ['Requested-Service-Unit', [
|
52 | ['CC-Time', 124],
|
53 | ['CC-Money', [
|
54 | ['Unit-Value', [
|
55 | ['Value-Digits', 123],
|
56 | ['Exponent', 1]
|
57 | ]],
|
58 | ['Currency-Code', 1]
|
59 | ]],
|
60 | ['CC-Total-Octets', 123],
|
61 | ['CC-Input-Octets', 123],
|
62 | ['CC-Output-Octets', 123]
|
63 | ]]
|
64 | ]]
|
65 | ];
|
66 |
|
67 | expect(util.getAvpValue(exampleMessage, '....')).toBe(undefined);
|
68 | expect(util.getAvpValue(exampleMessage, 'Origin-Realm')).toBe('com');
|
69 | expect(function() {
|
70 | util.getAvpValue(exampleMessage, 'Origin-Realm[1]');
|
71 | })
|
72 | .toThrow(new Error('Can\'t resolve path, index for \'Origin-Realm\' is out of bounds'));
|
73 | expect(util.getAvpValue(exampleMessage, 'Not-Present')).toBe(undefined);
|
74 | expect(util.getAvpValue(exampleMessage, 'Result-Code')).toBe(2001);
|
75 | expect(util.getAvpValue(exampleMessage,
|
76 | 'Multiple-Services-Credit-Control.Granted-Service-Unit.CC-Time'))
|
77 | .toBe(123);
|
78 | expect(util.getAvpValue(exampleMessage,
|
79 | 'Multiple-Services-Credit-Control.Granted-Service-Unit.Missing'))
|
80 | .toBe(undefined);
|
81 | expect(util.getAvpValue(exampleMessage,
|
82 | 'Multiple-Services-Credit-Control.Requested-Service-Unit[1].CC-Time'))
|
83 | .toBe(124);
|
84 | expect(function() {
|
85 | util.getAvpValue(exampleMessage,
|
86 | 'Multiple-Services-Credit-Control.Requested-Service-Unit.CC-Time');
|
87 | })
|
88 | .toThrow(new Error('Can\'t resolve path, multiple AVPs found with name \'Requested-Service-Unit\''));
|
89 | expect(util.getAvpValue(exampleMessage,
|
90 | 'Multiple-Services-Credit-Control.Requested-Service-Unit[1]'))
|
91 | .toDeepEqual([
|
92 | ['CC-Time', 124],
|
93 | ['CC-Money', [
|
94 | ['Unit-Value', [
|
95 | ['Value-Digits', 123],
|
96 | ['Exponent', 1]
|
97 | ]],
|
98 | ['Currency-Code', 1]
|
99 | ]],
|
100 | ['CC-Total-Octets', 123],
|
101 | ['CC-Input-Octets', 123],
|
102 | ['CC-Output-Octets', 123]
|
103 | ]);
|
104 | });
|
105 | });
|