...

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

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

     1  package testing
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"reflect"
     8  	"sort"
     9  	"strings"
    10  
    11  	"github.com/aws/smithy-go/testing/xml"
    12  )
    13  
    14  // JSONEqual compares two JSON documents and identifies if the documents contain
    15  // the same values. Returns an error if the two documents are not equal.
    16  func JSONEqual(expectBytes, actualBytes []byte) error {
    17  	var expect interface{}
    18  	if err := json.Unmarshal(expectBytes, &expect); err != nil {
    19  		return fmt.Errorf("failed to unmarshal expected bytes, %v", err)
    20  	}
    21  
    22  	var actual interface{}
    23  	if err := json.Unmarshal(actualBytes, &actual); err != nil {
    24  		return fmt.Errorf("failed to unmarshal actual bytes, %v", err)
    25  	}
    26  
    27  	if !reflect.DeepEqual(expect, actual) {
    28  		return fmt.Errorf("JSON mismatch: %v != %v", expect, actual)
    29  	}
    30  
    31  	return nil
    32  }
    33  
    34  // AssertJSONEqual compares two JSON documents and identifies if the documents
    35  // contain the same values. Emits a testing error, and returns false if the
    36  // documents are not equal.
    37  func AssertJSONEqual(t T, expect, actual []byte) bool {
    38  	t.Helper()
    39  
    40  	if err := JSONEqual(expect, actual); err != nil {
    41  		t.Errorf("expect JSON documents to be equal, %v", err)
    42  		return false
    43  	}
    44  
    45  	return true
    46  }
    47  
    48  // XMLEqual asserts two xml documents by sorting the XML and comparing the strings
    49  // It returns an error in case of mismatch or in case of malformed xml found while sorting.
    50  // In case of mismatched XML, the error string will contain the diff between the two XMLs.
    51  func XMLEqual(expectBytes, actualBytes []byte) error {
    52  	actualString, err := xml.SortXML(bytes.NewBuffer(actualBytes), true)
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	expectedString, err := xml.SortXML(bytes.NewBuffer(expectBytes), true)
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	if expectedString != actualString {
    63  		return fmt.Errorf("XML mismatch: %v != %v", expectedString, actualString)
    64  	}
    65  
    66  	return nil
    67  }
    68  
    69  // AssertXMLEqual compares two XML documents and identifies if the documents
    70  // contain the same values. Emits a testing error, and returns false if the
    71  // documents are not equal.
    72  func AssertXMLEqual(t T, expect, actual []byte) bool {
    73  	t.Helper()
    74  
    75  	if err := XMLEqual(expect, actual); err != nil {
    76  		t.Errorf("expect XML documents to be equal, %v", err)
    77  		return false
    78  	}
    79  
    80  	return true
    81  }
    82  
    83  // URLFormEqual compares two URLForm documents and identifies if the documents
    84  // contain the same values. Returns an error if the two documents are not
    85  // equal.
    86  func URLFormEqual(expectBytes, actualBytes []byte) error {
    87  	expect := parseFormBody(expectBytes)
    88  	actual := parseFormBody(actualBytes)
    89  	if !reflect.DeepEqual(expect, actual) {
    90  		return fmt.Errorf("Query mismatch: %v != %v", expect, actual)
    91  	}
    92  	return nil
    93  }
    94  
    95  func parseFormBody(bytes []byte) []QueryItem {
    96  	str := string(bytes)
    97  	// Strip out any whitespace. Significant whitespace will be encoded, and so
    98  	// won't be stripped.
    99  	str = strings.Join(strings.Fields(str), "")
   100  	parsed := ParseRawQuery(str)
   101  	sort.SliceStable(parsed, func(i, j int) bool {
   102  		return parsed[i].Key < parsed[j].Key
   103  	})
   104  	return parsed
   105  }
   106  
   107  // AssertURLFormEqual compares two URLForm documents and identifies if the
   108  // documents contain the same values. Emits a testing error, and returns false
   109  // if the documents are not equal.
   110  func AssertURLFormEqual(t T, expect, actual []byte) bool {
   111  	t.Helper()
   112  
   113  	if err := URLFormEqual(expect, actual); err != nil {
   114  		t.Errorf("expect URLForm documents to be equal, %v", err)
   115  		return false
   116  	}
   117  
   118  	return true
   119  }
   120  

View as plain text