MCPcopy
hub / github.com/nats-io/nats.go / TestWSDecompressor

Function TestWSDecompressor

ws_test.go:419–500  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

417}
418
419func TestWSDecompressor(t *testing.T) {
420 var br *wsDecompressor
421
422 p := make([]byte, 100)
423 checkRead := func(limit int, expected []byte) {
424 t.Helper()
425 n, err := br.Read(p[:limit])
426 if err != nil {
427 t.Fatalf("Error on read: %v", err)
428 }
429 if got := p[:n]; !bytes.Equal(expected, got) {
430 t.Fatalf("Expected %v, got %v", expected, got)
431 }
432 }
433 checkEOF := func() {
434 t.Helper()
435 n, err := br.Read(p)
436 if err != io.EOF || n > 0 {
437 t.Fatalf("Unexpected result: n=%v err=%v", n, err)
438 }
439 }
440 checkReadByte := func(expected byte) {
441 t.Helper()
442 b, err := br.ReadByte()
443 if err != nil {
444 t.Fatalf("Error on read: %v", err)
445 }
446 if b != expected {
447 t.Fatalf("Expected %c, got %c", expected, b)
448 }
449 }
450 checkEOFWithReadByte := func() {
451 t.Helper()
452 n, err := br.ReadByte()
453 if err != io.EOF || n > 0 {
454 t.Fatalf("Unexpected result: n=%v err=%v", n, err)
455 }
456 }
457
458 newDecompressor := func(str string) *wsDecompressor {
459 d := &wsDecompressor{}
460 d.addBuf([]byte(str))
461 return d
462 }
463
464 // Read with enough room
465 br = newDecompressor("ABCDE")
466 checkRead(100, []byte("ABCDE"))
467 checkEOF()
468 checkEOFWithReadByte()
469
470 // Read with a partial from our buffer
471 br = newDecompressor("FGHIJ")
472 checkRead(2, []byte("FG"))
473 // Call with more than the end of our buffer.
474 checkRead(10, []byte("HIJ"))
475 checkEOF()
476 checkEOFWithReadByte()

Callers

nothing calls this directly

Calls 5

ReadMethod · 0.95
ReadByteMethod · 0.95
addBufMethod · 0.95
FatalfMethod · 0.80
EqualMethod · 0.80

Tested by

no test coverage detected