...

Source file src/github.com/aws/smithy-go/testing/bytes.go

Documentation: github.com/aws/smithy-go/testing

     1  package testing
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"fmt"
     7  	"io"
     8  	"io/ioutil"
     9  )
    10  
    11  // Enumeration values for supported compress Algorithms.
    12  const (
    13  	GZIP = "gzip"
    14  )
    15  
    16  type compareCompressFunc func([]byte, io.Reader) error
    17  
    18  var allowedAlgorithms = map[string]compareCompressFunc{
    19  	GZIP: GzipCompareCompressBytes,
    20  }
    21  
    22  // CompareReaderEmpty checks if the reader is nil, or contains no bytes.
    23  // Returns an error if not empty.
    24  func CompareReaderEmpty(r io.Reader) error {
    25  	if r == nil {
    26  		return nil
    27  	}
    28  	b, err := ioutil.ReadAll(r)
    29  	if err != nil && err != io.EOF {
    30  		return fmt.Errorf("unable to read from reader, %v", err)
    31  	}
    32  	if len(b) != 0 {
    33  		return fmt.Errorf("reader not empty, got\n%v", hex.Dump(b))
    34  	}
    35  	return nil
    36  }
    37  
    38  // CompareReaderBytes compares the reader with the expected bytes. Returns an
    39  // error if the two bytes are not equal.
    40  func CompareReaderBytes(r io.Reader, expect []byte) error {
    41  	if r == nil {
    42  		return fmt.Errorf("missing body")
    43  	}
    44  	actual, err := ioutil.ReadAll(r)
    45  	if err != nil {
    46  		return fmt.Errorf("unable to read, %v", err)
    47  	}
    48  
    49  	if !bytes.Equal(expect, actual) {
    50  		return fmt.Errorf("bytes not equal\nexpect:\n%v\nactual:\n%v",
    51  			hex.Dump(expect), hex.Dump(actual))
    52  	}
    53  	return nil
    54  }
    55  
    56  // CompareJSONReaderBytes compares the reader containing serialized JSON
    57  // document. Deserializes the JSON documents to determine if they are equal.
    58  // Return an error if the two JSON documents are not equal.
    59  func CompareJSONReaderBytes(r io.Reader, expect []byte) error {
    60  	if r == nil {
    61  		return fmt.Errorf("missing body")
    62  	}
    63  	actual, err := ioutil.ReadAll(r)
    64  	if err != nil {
    65  		return fmt.Errorf("unable to read, %v", err)
    66  	}
    67  
    68  	if err := JSONEqual(expect, actual); err != nil {
    69  		return fmt.Errorf("JSON documents not equal, %v", err)
    70  	}
    71  	return nil
    72  }
    73  
    74  // CompareXMLReaderBytes compares the reader with expected xml byte
    75  func CompareXMLReaderBytes(r io.Reader, expect []byte) error {
    76  	if r == nil {
    77  		return fmt.Errorf("missing body")
    78  	}
    79  
    80  	actual, err := ioutil.ReadAll(r)
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	if err := XMLEqual(expect, actual); err != nil {
    86  		return fmt.Errorf("XML documents not equal, %w", err)
    87  	}
    88  	return nil
    89  }
    90  
    91  // CompareURLFormReaderBytes compares the reader containing serialized URLForm
    92  // document. Deserializes the URLForm documents to determine if they are equal.
    93  // Return an error if the two URLForm documents are not equal.
    94  func CompareURLFormReaderBytes(r io.Reader, expect []byte) error {
    95  	if r == nil {
    96  		return fmt.Errorf("missing body")
    97  	}
    98  	actual, err := ioutil.ReadAll(r)
    99  	if err != nil {
   100  		return fmt.Errorf("unable to read, %v", err)
   101  	}
   102  
   103  	if err := URLFormEqual(expect, actual); err != nil {
   104  		return fmt.Errorf("URL query forms not equal, %v", err)
   105  	}
   106  	return nil
   107  }
   108  
   109  // CompareCompressedBytes compares the request stream before and after possible request compression
   110  func CompareCompressedBytes(expect *bytes.Buffer, actual io.Reader, disable bool, min int64, algorithm string) error {
   111  	expectBytes := expect.Bytes()
   112  	if disable || int64(len(expectBytes)) < min {
   113  		actualBytes, err := io.ReadAll(actual)
   114  		if err != nil {
   115  			return fmt.Errorf("error while reading request: %q", err)
   116  		}
   117  		if e, a := expectBytes, actualBytes; !bytes.Equal(e, a) {
   118  			return fmt.Errorf("expect content to be %s, got %s", e, a)
   119  		}
   120  	} else {
   121  		compareFn := allowedAlgorithms[algorithm]
   122  		if compareFn == nil {
   123  			return fmt.Errorf("compress algorithm %s is not allowed", algorithm)
   124  		}
   125  		if err := compareFn(expectBytes, actual); err != nil {
   126  			return fmt.Errorf("error while comparing unzipped content: %q", err)
   127  		}
   128  	}
   129  
   130  	return nil
   131  }
   132  

View as plain text