github.com/openshift-online/ocm-sdk-go@v0.1.473/compression_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 // This file contains tests for the support for response body compression. 18 19 package sdk 20 21 import ( 22 "compress/gzip" 23 "net/http" 24 "time" 25 26 . "github.com/onsi/ginkgo/v2/dsl/core" // nolint 27 . "github.com/onsi/gomega" // nolint 28 29 "github.com/onsi/gomega/ghttp" 30 31 . "github.com/openshift-online/ocm-sdk-go/testing" // nolint 32 ) 33 34 var _ = Describe("Compression", func() { 35 var ( 36 server *ghttp.Server 37 connection *Connection 38 ) 39 40 BeforeEach(func() { 41 var err error 42 43 // Create the tokens: 44 token := MakeTokenString("Bearer", 5*time.Minute) 45 46 // Create the server: 47 server = MakeTCPServer() 48 49 // Create the connection: 50 connection, err = NewConnectionBuilder(). 51 Logger(logger). 52 URL(server.URL()). 53 Tokens(token). 54 Build() 55 Expect(err).ToNot(HaveOccurred()) 56 }) 57 58 AfterEach(func() { 59 // Close the connection: 60 err := connection.Close() 61 Expect(err).ToNot(HaveOccurred()) 62 63 // Stop the server: 64 server.Close() 65 }) 66 67 It("Decompresses response body", func() { 68 // Prepare the server: 69 server.AppendHandlers( 70 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 71 encoding := r.Header.Get("Accept-Encoding") 72 Expect(encoding).To(Equal("gzip")) 73 body := []byte(`{ 74 "kind": "Cluster", 75 "id": "123", 76 "href": "/api/clusters_mgmt/v1/clusters/123", 77 "name": "mycluster" 78 }`) 79 w.Header().Set("Content-Type", "application/json") 80 w.Header().Set("Content-Encoding", "gzip") 81 w.WriteHeader(http.StatusOK) 82 compressor := gzip.NewWriter(w) 83 _, err := compressor.Write(body) 84 Expect(err).ToNot(HaveOccurred()) 85 err = compressor.Close() 86 Expect(err).ToNot(HaveOccurred()) 87 }), 88 ) 89 90 // Send the request: 91 response, err := connection.ClustersMgmt().V1().Clusters().Cluster("123").Get(). 92 Send() 93 Expect(err).ToNot(HaveOccurred()) 94 Expect(response).ToNot(BeNil()) 95 result := response.Body() 96 Expect(result.Kind()).To(Equal("Cluster")) 97 Expect(result.ID()).To(Equal("123")) 98 Expect(result.HREF()).To(Equal("/api/clusters_mgmt/v1/clusters/123")) 99 Expect(result.Name()).To(Equal("mycluster")) 100 }) 101 })