github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/common/flogging/httpadmin/spec_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package httpadmin_test
     8  
     9  import (
    10  	"errors"
    11  	"net/http"
    12  	"net/http/httptest"
    13  	"strings"
    14  
    15  	"github.com/hyperledger/fabric/common/flogging"
    16  	"github.com/hyperledger/fabric/common/flogging/httpadmin"
    17  	"github.com/hyperledger/fabric/common/flogging/httpadmin/fakes"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var _ = Describe("SpecHandler", func() {
    23  	var (
    24  		fakeLogging *fakes.Logging
    25  		handler     *httpadmin.SpecHandler
    26  	)
    27  
    28  	BeforeEach(func() {
    29  		fakeLogging = &fakes.Logging{}
    30  		fakeLogging.SpecReturns("the-returned-specification")
    31  		handler = &httpadmin.SpecHandler{
    32  			Logging: fakeLogging,
    33  		}
    34  	})
    35  
    36  	It("responds with the current logging spec", func() {
    37  		req := httptest.NewRequest("GET", "/ignored", nil)
    38  		resp := httptest.NewRecorder()
    39  		handler.ServeHTTP(resp, req)
    40  
    41  		Expect(fakeLogging.SpecCallCount()).To(Equal(1))
    42  		Expect(resp.Code).To(Equal(http.StatusOK))
    43  		Expect(resp.Body).To(MatchJSON(`{"spec": "the-returned-specification"}`))
    44  		Expect(resp.Header().Get("Content-Type")).To(Equal("application/json"))
    45  	})
    46  
    47  	It("sets the current logging spec", func() {
    48  		req := httptest.NewRequest("PUT", "/ignored", strings.NewReader(`{"spec": "updated-spec"}`))
    49  		resp := httptest.NewRecorder()
    50  		handler.ServeHTTP(resp, req)
    51  
    52  		Expect(resp.Code).To(Equal(http.StatusNoContent))
    53  		Expect(fakeLogging.ActivateSpecCallCount()).To(Equal(1))
    54  		Expect(fakeLogging.ActivateSpecArgsForCall(0)).To(Equal("updated-spec"))
    55  	})
    56  
    57  	Context("when the update spec payload cannot be decoded", func() {
    58  		It("responds with an error payload", func() {
    59  			req := httptest.NewRequest("PUT", "/ignored", strings.NewReader(`goo`))
    60  			resp := httptest.NewRecorder()
    61  			handler.ServeHTTP(resp, req)
    62  
    63  			Expect(fakeLogging.ActivateSpecCallCount()).To(Equal(0))
    64  			Expect(resp.Code).To(Equal(http.StatusBadRequest))
    65  			Expect(resp.Body).To(MatchJSON(`{"error": "invalid character 'g' looking for beginning of value"}`))
    66  		})
    67  	})
    68  
    69  	Context("when activating the spec fails", func() {
    70  		BeforeEach(func() {
    71  			fakeLogging.ActivateSpecReturns(errors.New("ewww; that's not right!"))
    72  		})
    73  
    74  		It("responds with an error payload", func() {
    75  			req := httptest.NewRequest("PUT", "/ignored", strings.NewReader(`{}`))
    76  			resp := httptest.NewRecorder()
    77  			handler.ServeHTTP(resp, req)
    78  
    79  			Expect(resp.Code).To(Equal(http.StatusBadRequest))
    80  			Expect(resp.Body).To(MatchJSON(`{"error": "ewww; that's not right!"}`))
    81  		})
    82  	})
    83  
    84  	Context("when an unsupported method is used", func() {
    85  		It("responds with an error", func() {
    86  			req := httptest.NewRequest("POST", "/ignored", strings.NewReader(`{}`))
    87  			resp := httptest.NewRecorder()
    88  			handler.ServeHTTP(resp, req)
    89  
    90  			Expect(resp.Code).To(Equal(http.StatusBadRequest))
    91  			Expect(resp.Body).To(MatchJSON(`{"error": "invalid request method: POST"}`))
    92  		})
    93  
    94  		It("doesn't use logging", func() {
    95  			req := httptest.NewRequest("POST", "/ignored", strings.NewReader(`{}`))
    96  			resp := httptest.NewRecorder()
    97  			handler.ServeHTTP(resp, req)
    98  
    99  			Expect(fakeLogging.ActivateSpecCallCount()).To(Equal(0))
   100  			Expect(fakeLogging.SpecCallCount()).To(Equal(0))
   101  		})
   102  	})
   103  
   104  	Describe("NewSpecHandler", func() {
   105  		It("constructs a handler that modifies the global spec", func() {
   106  			specHandler := httpadmin.NewSpecHandler()
   107  			Expect(specHandler.Logging).To(Equal(flogging.Global))
   108  			Expect(specHandler.Logger).NotTo(BeNil())
   109  		})
   110  	})
   111  })