16
16
* See the License for the specific language governing permissions and
17
17
* limitations under the License.
18
18
*/
19
- var DummyChannel = require ( '../../lib/v1/internal/ch-dummy.js' ) ;
20
- var connect = require ( "../../lib/v1/internal/connector.js" ) . connect ;
21
19
22
- describe ( 'connector' , function ( ) {
20
+ import * as DummyChannel from '../../src/v1/internal/ch-dummy' ;
21
+ import { connect , Connection } from '../../src/v1/internal/connector' ;
22
+ import { Packer } from '../../src/v1/internal/packstream' ;
23
+ import { Chunker } from '../../src/v1/internal/chunking' ;
24
+ import { alloc } from '../../src/v1/internal/buf' ;
25
+ import { Neo4jError } from '../../src/v1/error' ;
23
26
24
- it ( 'should read/write basic messages' , function ( done ) {
27
+ describe ( 'connector' , ( ) => {
28
+
29
+ it ( 'should read/write basic messages' , done => {
25
30
// Given
26
- var conn = connect ( "bolt://localhost" )
31
+ const conn = connect ( "bolt://localhost" ) ;
27
32
28
33
// When
29
- conn . initialize ( "mydriver/0.0.0" , { scheme : "basic" , principal : "neo4j" , credentials : "neo4j" } , {
30
- onCompleted : function ( msg ) {
31
- expect ( msg ) . not . toBeNull ( ) ;
34
+ conn . initialize ( "mydriver/0.0.0" , { scheme : "basic" , principal : "neo4j" , credentials : "neo4j" } , {
35
+ onCompleted : msg => {
36
+ expect ( msg ) . not . toBeNull ( ) ;
32
37
conn . close ( ) ;
33
38
done ( ) ;
34
39
} ,
35
- onError : function ( err ) {
36
- console . log ( err ) ;
37
- }
40
+ onError : console . log
38
41
} ) ;
39
42
conn . sync ( ) ;
40
43
41
44
} ) ;
42
- it ( 'should retrieve stream' , function ( done ) {
45
+
46
+ it ( 'should retrieve stream' , done => {
43
47
// Given
44
- var conn = connect ( "bolt://localhost" )
48
+ const conn = connect ( "bolt://localhost" ) ;
45
49
46
50
// When
47
- var records = [ ] ;
48
- conn . initialize ( "mydriver/0.0.0" , { scheme : "basic" , principal : "neo4j" , credentials : "neo4j" } ) ;
49
- conn . run ( "RETURN 1.0" , { } ) ;
50
- conn . pullAll ( {
51
- onNext : function ( record ) {
52
- records . push ( record ) ;
51
+ const records = [ ] ;
52
+ conn . initialize ( "mydriver/0.0.0" , { scheme : "basic" , principal : "neo4j" , credentials : "neo4j" } ) ;
53
+ conn . run ( "RETURN 1.0" , { } ) ;
54
+ conn . pullAll ( {
55
+ onNext : record => {
56
+ records . push ( record ) ;
53
57
} ,
54
- onCompleted : function ( tail ) {
55
- expect ( records [ 0 ] [ 0 ] ) . toBe ( 1 ) ;
58
+ onCompleted : ( ) => {
59
+ expect ( records [ 0 ] [ 0 ] ) . toBe ( 1 ) ;
56
60
conn . close ( ) ;
57
61
done ( ) ;
58
62
}
59
63
} ) ;
60
64
conn . sync ( ) ;
61
65
} ) ;
62
66
63
- it ( 'should use DummyChannel to read what gets written' , function ( done ) {
67
+ it ( 'should use DummyChannel to read what gets written' , done => {
64
68
// Given
65
- var observer = DummyChannel . observer ;
66
- var conn = connect ( "bolt://localhost" , { channel :DummyChannel . channel } ) ;
69
+ const observer = DummyChannel . observer ;
70
+ const conn = connect ( "bolt://localhost" , { channel : DummyChannel . channel } ) ;
67
71
68
72
// When
69
- var records = [ ] ;
70
- conn . initialize ( "mydriver/0.0.0" , { scheme : "basic" , principal : "neo4j" , credentials : "neo4j" } ) ;
71
- conn . run ( "RETURN 1" , { } ) ;
73
+ conn . initialize ( "mydriver/0.0.0" , { scheme : "basic" , principal : "neo4j" , credentials : "neo4j" } ) ;
74
+ conn . run ( "RETURN 1" , { } ) ;
72
75
conn . sync ( ) ;
73
- expect ( observer . instance . toHex ( ) ) . toBe ( '60 60 b0 17 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 41 b2 01 8e 6d 79 64 72 69 76 65 72 2f 30 2e 30 2e 30 a3 86 73 63 68 65 6d 65 85 62 61 73 69 63 89 70 72 69 6e 63 69 70 61 6c 85 6e 65 6f 34 6a 8b 63 72 65 64 65 6e 74 69 61 6c 73 85 6e 65 6f 34 6a 00 00 00 0c b2 10 88 52 45 54 55 52 4e 20 31 a0 00 00 ' ) ;
76
+ expect ( observer . instance . toHex ( ) ) . toBe ( '60 60 b0 17 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 41 b2 01 8e 6d 79 64 72 69 76 65 72 2f 30 2e 30 2e 30 a3 86 73 63 68 65 6d 65 85 62 61 73 69 63 89 70 72 69 6e 63 69 70 61 6c 85 6e 65 6f 34 6a 8b 63 72 65 64 65 6e 74 69 61 6c 73 85 6e 65 6f 34 6a 00 00 00 0c b2 10 88 52 45 54 55 52 4e 20 31 a0 00 00 ' ) ;
74
77
done ( ) ;
75
78
} ) ;
76
79
77
- it ( 'should provide error message when connecting to http-port' , function ( done ) {
80
+ it ( 'should provide error message when connecting to http-port' , done => {
78
81
// Given
79
- var conn = connect ( "bolt://localhost:7474" , { encrypted :false } ) ;
82
+ const conn = connect ( "bolt://localhost:7474" , { encrypted : false } ) ;
80
83
81
84
// When
82
- conn . initialize ( "mydriver/0.0.0" , { scheme : "basic" , principal : "neo4j" , credentials : "neo4j" } , {
83
- onCompleted : function ( msg ) {
85
+ conn . initialize ( "mydriver/0.0.0" , { scheme : "basic" , principal : "neo4j" , credentials : "neo4j" } , {
86
+ onCompleted : msg => {
84
87
} ,
85
- onError : function ( err ) {
88
+ onError : err => {
86
89
//only node gets the pretty error message
87
- if ( require ( '../../lib/v1/internal/ch-node.js' ) . available ) {
90
+ if ( require ( '../../lib/v1/internal/ch-node.js' ) . available ) {
88
91
expect ( err . message ) . toBe ( "Server responded HTTP. Make sure you are not trying to connect to the http endpoint " +
89
92
"(HTTP defaults to port 7474 whereas BOLT defaults to port 7687)" ) ;
90
93
}
@@ -95,4 +98,48 @@ describe('connector', function() {
95
98
96
99
} ) ;
97
100
101
+ it ( 'should convert failure messages to errors' , done => {
102
+ const channel = new DummyChannel . channel ;
103
+ const connection = new Connection ( channel , 'bolt://localhost' ) ;
104
+
105
+ const errorCode = 'Neo.ClientError.Schema.ConstraintValidationFailed' ;
106
+ const errorMessage = 'Node 0 already exists with label User and property "email"=[[email protected] ]' ;
107
+
108
+ connection . _queueObserver ( {
109
+ onError : error => {
110
+ expectNeo4jError ( error , errorCode , errorMessage ) ;
111
+ done ( ) ;
112
+ }
113
+ } ) ;
114
+
115
+ channel . onmessage ( packedHandshakeMessage ( ) ) ;
116
+ channel . onmessage ( packedFailureMessage ( errorCode , errorMessage ) ) ;
117
+ } ) ;
118
+
119
+ function packedHandshakeMessage ( ) {
120
+ const result = alloc ( 4 ) ;
121
+ result . putInt32 ( 0 , 1 ) ;
122
+ result . reset ( ) ;
123
+ return result ;
124
+ }
125
+
126
+ function packedFailureMessage ( code , message ) {
127
+ const channel = new DummyChannel . channel ;
128
+ const chunker = new Chunker ( channel ) ;
129
+ const packer = new Packer ( chunker ) ;
130
+ packer . packStruct ( 0x7F , [ packer . packable ( { code : code , message : message } ) ] ) ;
131
+ chunker . messageBoundary ( ) ;
132
+ chunker . flush ( ) ;
133
+ const data = channel . toBuffer ( ) ;
134
+ const result = alloc ( data . length ) ;
135
+ result . putBytes ( 0 , data ) ;
136
+ return result ;
137
+ }
138
+
139
+ function expectNeo4jError ( error , expectedCode , expectedMessage ) {
140
+ expect ( ( ) => {
141
+ throw error ;
142
+ } ) . toThrow ( new Neo4jError ( expectedMessage , expectedCode ) ) ;
143
+ }
144
+
98
145
} ) ;
0 commit comments