Ejemplo n.º 1
0
func init() {
	go func() {
		// falcore setup
		pipeline := falcore.NewPipeline()
		pipeline.Upstream.PushBack(falcore.NewRequestFilter(func(req *falcore.Request) *http.Response {
			for _, data := range eserverData {
				if data.path == req.HttpRequest.URL.Path {
					header := make(http.Header)
					header.Set("Etag", data.etag)
					if data.chunked {
						buf := new(bytes.Buffer)
						buf.Write(data.body)
						res := falcore.SimpleResponse(req.HttpRequest, data.status, header, -1, ioutil.NopCloser(buf))
						res.TransferEncoding = []string{"chunked"}
						return res
					} else {
						return falcore.StringResponse(req.HttpRequest, data.status, header, string(data.body))
					}
				}
			}
			return falcore.StringResponse(req.HttpRequest, 404, nil, "Not Found")
		}))

		pipeline.Downstream.PushBack(new(EtagFilter))

		esrv = falcore.NewServer(0, pipeline)
		if err := esrv.ListenAndServe(); err != nil {
			panic("Could not start falcore")
		}
	}()
}
Ejemplo n.º 2
0
// Generate an http.Response by json encoding body using
// the standard library's json.Encoder.  error will be nil
// unless json encoding fails.
func JSONResponse(req *http.Request, status int, headers http.Header, body interface{}) (*http.Response, error) {
	buf := new(bytes.Buffer)
	if err := json.NewEncoder(buf).Encode(body); err != nil {
		return nil, err
	}

	if headers == nil {
		headers = make(http.Header)
	}
	if headers.Get("Content-Type") == "" {
		headers.Set("Content-Type", "application/json")
	}

	return falcore.SimpleResponse(req, status, headers, int64(buf.Len()), buf), nil
}
Ejemplo n.º 3
0
func TestUpstreamTimeout(t *testing.T) {
	// Start a test server
	sleepPipe := falcore.NewPipeline()
	sleepPipe.Upstream.PushBack(falcore.NewRequestFilter(func(req *falcore.Request) *http.Response {
		tt, _ := strconv.Atoi(req.HttpRequest.URL.Query().Get("time"))
		b, _ := strconv.Atoi(req.HttpRequest.URL.Query().Get("body"))
		bl, _ := strconv.Atoi(req.HttpRequest.URL.Query().Get("bl"))

		time.Sleep(time.Duration(tt))
		pr, pw := io.Pipe()
		go func() {
			buf := make([]byte, 1024)
			for i := 0; i < bl; i++ {
				<-time.After(time.Duration(b))
				pw.Write(buf)
			}
			pw.Close()
		}()
		return falcore.SimpleResponse(req.HttpRequest, 200, nil, int64(bl*1024), pr)
	}))
	sleepSrv := falcore.NewServer(0, sleepPipe)
	go func() {
		sleepSrv.ListenAndServe()
	}()
	<-sleepSrv.AcceptReady

	// Build Upstream
	up := NewUpstream(NewUpstreamTransport("localhost", sleepSrv.Port(), time.Second, nil))

	for _, test := range upstreamTimeoutTestData {
		req, _ := http.NewRequest("GET", fmt.Sprintf("http://localhost/test?time=%v&body=%v&bl=%v", int64(test.Time), int64(test.BodyTime), test.BodyLen), nil)
		_, res := falcore.TestWithRequest(req, up, nil)
		if res.StatusCode != test.StatusCode {
			t.Errorf("%v Expected status %v Got %v", test.Name, test.StatusCode, res.StatusCode)
		}

		if res.StatusCode == 200 {
			i, _ := io.Copy(ioutil.Discard, res.Body)
			res.Body.Close()
			if i != (test.BodyLen * 1024) {
				t.Errorf("%v Expected body len %v Got %v", test.Name, (test.BodyLen * 1024), i)
			}
		}
	}
}
Ejemplo n.º 4
0
			if err := rows.Scan(&fortune.Id, &fortune.Message); err != nil {
				log.Fatalf("Error scanning fortune row: %s", err.Error())
			}
			fortunes = append(fortunes, &fortune)
		}
		fortunes = append(fortunes, &Fortune{Message: "Additional fortune added at request time."})

		sort.Sort(ByMessage{fortunes})
		pipeReader, pipeWriter := io.Pipe()
		// TODO maybe figure out err handling
		go func() {
			tmpl.Execute(pipeWriter, fortunes)
			pipeWriter.Close()
		}()

		return falcore.SimpleResponse(req.HttpRequest, 200, textHtml, -1, pipeReader)
	}
	return nil
})

// Test 5: Database updates
var updateFilter = falcore.NewRequestFilter(func(req *falcore.Request) *http.Response {
	if req.HttpRequest.URL.Path == "/update" {
		n := 1
		if nStr := req.HttpRequest.URL.Query().Get("queries"); len(nStr) > 0 {
			n, _ = strconv.Atoi(nStr)
		}

		if n <= 1 {
			var world World
			worldStatement.QueryRow(rand.Intn(worldRowCount)+1).Scan(&world.Id, &world.RandomNumber)
Ejemplo n.º 5
0
// A 302 redirect response
func RedirectResponse(req *http.Request, url string) *http.Response {
	h := make(http.Header)
	h.Set("Location", url)
	return falcore.SimpleResponse(req, 302, h, 0, nil)
}