Skip to content
Snippets Groups Projects
Select Git revision
  • 96dfef9c9e87c841897b6c8347e780eb59e99bdd
  • release default protected
  • 11-22-implement-kv-interface-defined-in-collectiveversionedkvgo
  • hotfix/TestHostPool_UpdateNdf_AddFilter
  • XX-4719/announcementChannels
  • xx-4717/logLevel
  • jonah/noob-channel
  • master protected
  • XX-4707/tagDiskJson
  • xx-4698/notification-retry
  • hotfix/notifylockup
  • syncNodes
  • hotfix/localCB
  • XX-4677/NewChanManagerMobile
  • XX-4689/DmSync
  • duplicatePrefix
  • XX-4601/HavenInvites
  • finalizedUICallbacks
  • XX-4673/AdminKeySync
  • debugNotifID
  • anne/test
  • v4.7.5
  • v4.7.4
  • v4.7.3
  • v4.7.2
  • v4.7.1
  • v4.6.3
  • v4.6.1
  • v4.5.0
  • v4.4.4
  • v4.3.11
  • v4.3.8
  • v4.3.7
  • v4.3.6
  • v4.3.5
  • v4.2.0
  • v4.3.0
  • v4.3.4
  • v4.3.3
  • v4.3.2
  • v4.3.1
41 results

response_test.go

Blame
  • user avatar
    Jake Taylor authored
    fa2a17d9
    History
    response_test.go 2.43 KiB
    ////////////////////////////////////////////////////////////////////////////////
    // Copyright © 2022 Privategrity Corporation                                   /
    //                                                                             /
    // All rights reserved.                                                        /
    ////////////////////////////////////////////////////////////////////////////////
    
    package restlike
    
    import (
    	"bytes"
    	"github.com/pkg/errors"
    	"gitlab.com/elixxir/client/cmix/identity/receptionID"
    	"google.golang.org/protobuf/proto"
    	"testing"
    	"time"
    )
    
    // Test happy path
    func TestSingleResponse_Callback(t *testing.T) {
    	resultChan := make(chan *Message, 1)
    	cb := func(input *Message) {
    		resultChan <- input
    	}
    	testPath := "test/path"
    	testMethod := Get
    	testMessage := &Message{
    		Content: []byte("test"),
    		Headers: nil,
    		Method:  uint32(testMethod),
    		Uri:     testPath,
    		Error:   "",
    	}
    
    	response := singleResponse{cb}
    
    	testPayload, err := proto.Marshal(testMessage)
    	if err != nil {
    		t.Errorf(err.Error())
    	}
    	response.Callback(testPayload, receptionID.EphemeralIdentity{}, nil, nil)
    
    	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 error input path
    func TestSingleResponse_Callback_Err(t *testing.T) {
    	resultChan := make(chan *Message, 1)
    	cb := func(input *Message) {
    		resultChan <- input
    	}
    	response := singleResponse{cb}
    
    	response.Callback(nil, receptionID.EphemeralIdentity{}, nil, errors.New("test"))
    
    	select {
    	case result := <-resultChan:
    		if len(result.Error) == 0 {
    			t.Errorf("Expected cb error!")
    		}
    	case <-time.After(3 * time.Second):
    		t.Errorf("Test SingleResponse input error timed out!")
    	}
    }
    
    // Test proto error path
    func TestSingleResponse_Callback_ProtoErr(t *testing.T) {
    	resultChan := make(chan *Message, 1)
    	cb := func(input *Message) {
    		resultChan <- input
    	}
    	response := singleResponse{cb}
    
    	response.Callback([]byte("test"), receptionID.EphemeralIdentity{}, nil, nil)
    
    	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!")
    	}
    }