istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/json.go (about) 1 // Copyright Istio Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package test 16 17 import ( 18 "bytes" 19 "encoding/json" 20 ) 21 22 // JSONEquals compares two json strings. We cannot compare JSON strings from protobuf because of 23 // design decisions https://github.com/golang/protobuf/issues/1373 Instead, use this function to 24 // normalize the formatting 25 func JSONEquals(t Failer, a, b string) { 26 t.Helper() 27 ba := bytes.Buffer{} 28 if err := json.Compact(&ba, []byte(a)); err != nil { 29 t.Fatal(err) 30 } 31 bb := bytes.Buffer{} 32 if err := json.Compact(&bb, []byte(b)); err != nil { 33 t.Fatal(err) 34 } 35 if ba.String() != bb.String() { 36 t.Fatalf("got %v, want %v", ba.String(), bb.String()) 37 } 38 }