github.com/openshift-online/ocm-sdk-go@v0.1.473/dump_test.go (about)

     1  /*
     2  Copyright (c) 2021 Red Hat, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8    http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package sdk
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  
    23  	. "github.com/onsi/ginkgo/v2/dsl/core" // nolint
    24  	. "github.com/onsi/gomega"             // nolint
    25  )
    26  
    27  var _ = Describe("DumpRoundTripper", func() {
    28  	var stdOut bytes.Buffer
    29  	var stdErr bytes.Buffer
    30  	var stdLogger *StdLogger
    31  	var d *dumpRoundTripper
    32  
    33  	BeforeEach(func() {
    34  		var err error
    35  
    36  		// Clear buffers
    37  		stdOut.Reset()
    38  		stdErr.Reset()
    39  
    40  		// Create logger which writes to buffers
    41  		stdLogger, err = NewStdLoggerBuilder().
    42  			Streams(&stdOut, &stdErr).
    43  			Debug(true).
    44  			Build()
    45  		Expect(err).ToNot(HaveOccurred())
    46  
    47  		d = &dumpRoundTripper{stdLogger, nil}
    48  	})
    49  
    50  	It("dumpJson ordering test", func() {
    51  		d := dumpRoundTripper{stdLogger, nil}
    52  		json := `{ "z": 0, "y": null, "a": { "a": 5 }, "b": [ 1, { "a": 5 }, 3], "c": true }`
    53  		d.dumpJSON(context.Background(), []byte(json))
    54  		expectedJSON := `{
    55    "z": 0,
    56    "y": null,
    57    "a": {
    58      "a": 5
    59    },
    60    "b": [
    61      1,
    62      {
    63        "a": 5
    64      },
    65      3
    66    ],
    67    "c": true
    68  }
    69  `
    70  		Expect(stdOut.String()).To(Equal(expectedJSON))
    71  	})
    72  
    73  	It("dumpJson redacting test", func() {
    74  		json := `{ "z": 0, "access_token": null, "a": { "a": 5 }, "b": [ 1, { "password": 5 }, 3], "ssh": true }`
    75  		d.dumpJSON(context.Background(), []byte(json))
    76  		expectedJSON := `{
    77    "z": 0,
    78    "access_token": "***",
    79    "a": {
    80      "a": 5
    81    },
    82    "b": [
    83      1,
    84      {
    85        "password": "***"
    86      },
    87      3
    88    ],
    89    "ssh": "***"
    90  }
    91  `
    92  		Expect(stdOut.String()).To(Equal(expectedJSON))
    93  	})
    94  
    95  	It("dumpJson empty test", func() {
    96  		d := dumpRoundTripper{stdLogger, nil}
    97  		json := ``
    98  		d.dumpJSON(context.Background(), []byte(json))
    99  		expectedJSON := `
   100  `
   101  		Expect(stdOut.String()).To(Equal(expectedJSON))
   102  	})
   103  
   104  	It("dumpJson empty object test", func() {
   105  		d := dumpRoundTripper{stdLogger, nil}
   106  		json := `{}`
   107  		d.dumpJSON(context.Background(), []byte(json))
   108  		expectedJSON := "{\n  \n}\n"
   109  		Expect(stdOut.String()).To(Equal(expectedJSON))
   110  	})
   111  })