2014-06-06 23:40:48 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2014-06-23 18:32:11 +00:00
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2014-06-09 14:16:43 +00:00
|
|
|
// TestInterface is a simple interface providing Errorf, to make injection for
|
|
|
|
// testing easier (insert 'yo dawg' meme here)
|
|
|
|
type TestInterface interface {
|
|
|
|
Errorf(format string, args ...interface{})
|
|
|
|
}
|
2014-06-12 19:37:35 +00:00
|
|
|
type LogInterface interface {
|
|
|
|
Logf(format string, args ...interface{})
|
|
|
|
}
|
2014-06-09 14:16:43 +00:00
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
// FakeHandler is to assist in testing HTTP requests.
|
|
|
|
type FakeHandler struct {
|
|
|
|
RequestReceived *http.Request
|
2014-06-18 00:36:27 +00:00
|
|
|
RequestBody string
|
2014-06-06 23:40:48 +00:00
|
|
|
StatusCode int
|
|
|
|
ResponseBody string
|
2014-06-12 19:37:35 +00:00
|
|
|
// For logging - you can use a *testing.T
|
|
|
|
// This will keep log messages associated with the test.
|
|
|
|
T LogInterface
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FakeHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
|
|
|
|
f.RequestReceived = request
|
|
|
|
response.WriteHeader(f.StatusCode)
|
|
|
|
response.Write([]byte(f.ResponseBody))
|
|
|
|
|
|
|
|
bodyReceived, err := ioutil.ReadAll(request.Body)
|
2014-06-12 19:37:35 +00:00
|
|
|
if err != nil && f.T != nil {
|
|
|
|
f.T.Logf("Received read error: %#v", err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-18 00:36:27 +00:00
|
|
|
f.RequestBody = string(bodyReceived)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 14:16:43 +00:00
|
|
|
func (f FakeHandler) ValidateRequest(t TestInterface, expectedPath, expectedMethod string, body *string) {
|
2014-06-06 23:40:48 +00:00
|
|
|
if f.RequestReceived.URL.Path != expectedPath {
|
2014-06-07 11:38:16 +00:00
|
|
|
t.Errorf("Unexpected request path for request %#v, received: %q, expected: %q", f.RequestReceived, f.RequestReceived.URL.Path, expectedPath)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
if f.RequestReceived.Method != expectedMethod {
|
2014-06-07 11:38:16 +00:00
|
|
|
t.Errorf("Unexpected method: %q, expected: %q", f.RequestReceived.Method, expectedMethod)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
if body != nil {
|
2014-06-18 00:36:27 +00:00
|
|
|
if *body != f.RequestBody {
|
|
|
|
t.Errorf("Received body:\n%s\n Doesn't match expected body:\n%s", f.RequestBody, *body)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|