...

Source file src/go.etcd.io/etcd/server/v3/etcdserver/api/v2error/error_test.go

Documentation: go.etcd.io/etcd/server/v3/etcdserver/api/v2error

     1  // Copyright 2015 The etcd 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 v2error
    16  
    17  import (
    18  	"net/http"
    19  	"net/http/httptest"
    20  	"reflect"
    21  	"strings"
    22  	"testing"
    23  )
    24  
    25  func TestErrorWriteTo(t *testing.T) {
    26  	for k := range errors {
    27  		err := NewError(k, "", 1)
    28  		rr := httptest.NewRecorder()
    29  		err.WriteTo(rr)
    30  
    31  		if err.StatusCode() != rr.Code {
    32  			t.Errorf("HTTP status code %d, want %d", rr.Code, err.StatusCode())
    33  		}
    34  
    35  		gbody := strings.TrimSuffix(rr.Body.String(), "\n")
    36  		if err.toJsonString() != gbody {
    37  			t.Errorf("HTTP body %q, want %q", gbody, err.toJsonString())
    38  		}
    39  
    40  		wheader := http.Header(map[string][]string{
    41  			"Content-Type": {"application/json"},
    42  			"X-Etcd-Index": {"1"},
    43  		})
    44  
    45  		if !reflect.DeepEqual(wheader, rr.HeaderMap) {
    46  			t.Errorf("HTTP headers %v, want %v", rr.HeaderMap, wheader)
    47  		}
    48  	}
    49  
    50  }
    51  

View as plain text