Skip to content
Snippets Groups Projects
Select Git revision
  • 067d3df6c819ab71775268453fded5da49e20a21
  • master default
  • notifications_test_ndf
  • main_ud_local_ndf
  • crust-integration
  • new-client
  • internal_build
  • FE-1054_test_net_ndf
  • FE-1053_change_retry_behavior
  • app_modularization_refactor
  • FE_1020_remove_version_check
  • FE-992_android_migration
  • XX-4094_add_fact_panic
  • FE-990_retry_reset_request
  • development
  • 2.92
  • 2.9
  • 2.8
  • 2.7
  • 2.5
  • 2.3
  • 2.2
  • 2.1
  • 2.04
  • 2.03
  • 2.02
26 results

README.md

Blame
  • response_test.go 1.92 KiB
    ////////////////////////////////////////////////////////////////////////////////
    // Copyright © 2022 Privategrity Corporation                                   /
    //                                                                             /
    // All rights reserved.                                                        /
    ////////////////////////////////////////////////////////////////////////////////
    
    package connect
    
    import (
    	"bytes"
    	"gitlab.com/elixxir/client/e2e/receive"
    	"gitlab.com/elixxir/client/restlike"
    	"google.golang.org/protobuf/proto"
    	"testing"
    	"time"
    )
    
    // Test happy path
    func TestSingleResponse_Callback(t *testing.T) {
    	resultChan := make(chan *restlike.Message, 1)
    	cb := func(input *restlike.Message) {
    		resultChan <- input
    	}
    	testPath := "test/path"
    	testMethod := restlike.Get
    	testMessage := &restlike.Message{
    		Content: []byte("test"),
    		Headers: nil,
    		Method:  uint32(testMethod),
    		Uri:     testPath,
    		Error:   "",
    	}
    
    	r := response{cb}
    
    	testPayload, err := proto.Marshal(testMessage)
    	if err != nil {
    		t.Errorf(err.Error())
    	}
    	r.Hear(receive.Message{Payload: testPayload})
    
    	select {
    	case result := <-resultChan:
    		if result.Uri != testPath {
    			t.Errorf("Mismatched uri")
    		}
    		if result.Method != uint32(testMethod) {
    			t.Errorf("Mismatched method")
    		}
    		if !bytes.Equal(testMessage.Content, result.Content) {
    			t.Errorf("Mismatched content")
    		}
    	case <-time.After(3 * time.Second):
    		t.Errorf("Test SingleResponse timed out!")
    	}
    }
    
    // Test proto error path
    func TestSingleResponse_Callback_ProtoErr(t *testing.T) {
    	resultChan := make(chan *restlike.Message, 1)
    	cb := func(input *restlike.Message) {
    		resultChan <- input
    	}
    	r := response{cb}
    
    	r.Hear(receive.Message{Payload: []byte("test")})
    
    	select {
    	case result := <-resultChan:
    		if len(result.Error) == 0 {
    			t.Errorf("Expected cb proto error!")
    		}
    	case <-time.After(3 * time.Second):
    		t.Errorf("Test SingleResponse proto error timed out!")
    	}
    }