Пример #1
0
func resetTest(T *testlib.T) {
	isSetup = sync.Once{}
	if fd != nil {
		T.ExpectSuccess(fd.Close())
		fd = nil
	}
	if writerCmd != nil {
		T.ExpectSuccess(writerCmd.Wait())
		writerCmd = nil
	}
}
Пример #2
0
// Tuns function starts an HTTP server on localhost on a random port. This
// server will run in a goroutine until the listener returned here gets
// closed. Its in the best interest of the caller to defer a close to ensure
// that the http server goroutine gets shut down properly.
func runHttpServer(T *testlib.T) net.Listener {
	listener, err := net.Listen("tcp", ":0")
	T.ExpectSuccess(err)
	T.NotEqual(listener, nil)

	// Setup the HTTP server.
	s := &http.Server{
		Handler:        &httpHandler{},
		ReadTimeout:    1 * time.Second,
		WriteTimeout:   1 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}
	go func() {
		s.Serve(listener)
	}()

	// Return the listener object.
	return listener
}
Пример #3
0
// Runs through all of the known tests and collects the results from each
// one to be returned in the savedQuery slice.
func runTests(
	T *testlib.T, rt http.RoundTripper, addr, username, password string,
) []*savedQuery {
	makeRequest := func(addr, path, method, body string) *savedQuery {
		sq := new(savedQuery)
		var err error

		// Setup the Request
		sq.Request = new(http.Request)
		sq.Request.Method = method
		sq.Request.URL, err = url.Parse(fmt.Sprintf("http://%s%s", addr, path))
		T.ExpectSuccess(err)
		sq.Request.Header = make(http.Header, 1)
		sq.Request.Header.Add("X-Client-Header", "test")
		sq.Request.ContentLength = int64(len(body))
		if username != "" || password != "" {
			sq.Request.SetBasicAuth(username, password)
		}

		// Setup the Body
		bodyBuffer := &bytesBufferCloser{}
		_, err = bodyBuffer.Write([]byte(body))
		T.ExpectSuccess(err)
		sq.Request.Body = bodyBuffer
		sq.RequestBody = []byte(body)

		// Setup the HTTP client.
		client := &http.Client{}
		client.Transport = rt
		resp, err := client.Do(sq.Request)

		// Copy the results into the rr object.
		sq.Response = resp
		sq.Error = err
		if resp != nil {
			buffer := bytes.NewBuffer(nil)
			_, sq.ResponseBodyError = io.Copy(buffer, resp.Body)
			sq.ResponseBody = buffer.Bytes()
			sq.Response.Body = nil
		}

		// Nil out the request body.
		sq.Request.Body = nil

		// Success
		return sq
	}

	// Makes a request with an erroring client body.
	makeErrored := func(addr, path string) *savedQuery {
		sq := new(savedQuery)
		var err error

		// Setup the Request
		sq.Request = new(http.Request)
		sq.Request.Method = "GET"
		sq.Request.URL, err = url.Parse(fmt.Sprintf("http://%s%s", addr, path))
		T.ExpectSuccess(err)
		sq.Request.Header = make(http.Header, 1)
		sq.Request.Header.Add("X-Client-Header", "test")

		// Setup the Body
		bodyReader, bodyWriter := io.Pipe()
		sq.Request.Body = bodyReader
		sq.RequestBody = []byte("testfailure")
		sq.RequestBodyError = fmt.Errorf("expected")
		go func() {
			bodyWriter.Write(sq.RequestBody)
			bodyWriter.CloseWithError(sq.RequestBodyError)
		}()

		// Setup the HTTP client.
		client := &http.Client{}
		client.Transport = rt
		resp, err := client.Do(sq.Request)

		// Copy the results into the rr object.
		sq.Response = resp
		sq.Error = err
		if resp != nil {
			buffer := bytes.NewBuffer(nil)
			_, sq.ResponseBodyError = io.Copy(buffer, resp.Body)
			sq.ResponseBody = buffer.Bytes()
			sq.Response.Body = nil
		}

		// Nil out the request body.
		sq.Request.Body = nil

		// Success
		return sq
	}

	r := make([]*savedQuery, 0, 100)

	// Start with the simple status requests.
	for _, method := range []string{"GET", "POST", "HEAD"} {
		for _, path := range []string{"/201", "/220", "/404", "/501", "/540"} {
			r = append(r, makeRequest(addr, path, method, "body1"))
			r = append(r, makeRequest(addr, path, method, "body2"))
		}
	}

	for _, path := range []string{"/resp_header", "/body", "/error"} {
		r = append(r, makeRequest(addr, path, "GET", "body1"))
		r = append(r, makeRequest(addr, path, "GET", "body2"))
	}

	// Test the error on Request.Body.Read()
	r = append(r, makeErrored(addr, "/"))

	// Return the results.
	return r
}