error_test.go 1.3 KB

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