Skip to content

Commit

Permalink
feat: minimize diff with upstream
Browse files Browse the repository at this point in the history
This diff attempts to minimize the diff with upstream. We can
definitely further minimize it, but this is a first attempt at
making sure we stay as close as possible to it.
  • Loading branch information
bassosimone committed Mar 26, 2024
1 parent 52f7bf2 commit 0628efa
Show file tree
Hide file tree
Showing 47 changed files with 462 additions and 75 deletions.
2 changes: 1 addition & 1 deletion alpn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"testing"

. "github.com/ooni/oohttp"
"github.com/ooni/oohttp/httptest"
httptest "github.com/ooni/oohttp/httptest"
)

func TestNextProtoUpgrade(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cgi/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"strconv"
"strings"

"github.com/ooni/oohttp"
http "github.com/ooni/oohttp"
)

// Request returns the HTTP request as represented in the current
Expand Down
2 changes: 1 addition & 1 deletion cgi/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"strconv"
"strings"

"github.com/ooni/oohttp"
http "github.com/ooni/oohttp"
"golang.org/x/net/http/httpguts"
)

Expand Down
4 changes: 2 additions & 2 deletions cgi/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"testing"
"time"

"github.com/ooni/oohttp"
"github.com/ooni/oohttp/httptest"
http "github.com/ooni/oohttp"
httptest "github.com/ooni/oohttp/httptest"
)

func newRequest(httpreq string) *http.Request {
Expand Down
150 changes: 148 additions & 2 deletions cgi/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,60 @@
package cgi

import (
"bytes"
"errors"
"fmt"
"io"
"net/url"
"os"
"strings"
"testing"
"time"

"github.com/ooni/oohttp"
"github.com/ooni/oohttp/httptest"
http "github.com/ooni/oohttp"
httptest "github.com/ooni/oohttp/httptest"
testenv "github.com/ooni/oohttp/internal/testenv"
)

// This test is a CGI host (testing host.go) that runs its own binary
// as a child process testing the other half of CGI (child.go).
func TestHostingOurselves(t *testing.T) {
testenv.MustHaveExec(t)

h := &Handler{
Path: os.Args[0],
Root: "/test.go",
Args: []string{"-test.run=TestBeChildCGIProcess"},
}
expectedMap := map[string]string{
"test": "Hello CGI-in-CGI",
"param-a": "b",
"param-foo": "bar",
"env-GATEWAY_INTERFACE": "CGI/1.1",
"env-HTTP_HOST": "example.com",
"env-PATH_INFO": "",
"env-QUERY_STRING": "foo=bar&a=b",
"env-REMOTE_ADDR": "1.2.3.4",
"env-REMOTE_HOST": "1.2.3.4",
"env-REMOTE_PORT": "1234",
"env-REQUEST_METHOD": "GET",
"env-REQUEST_URI": "/test.go?foo=bar&a=b",
"env-SCRIPT_FILENAME": os.Args[0],
"env-SCRIPT_NAME": "/test.go",
"env-SERVER_NAME": "example.com",
"env-SERVER_PORT": "80",
"env-SERVER_SOFTWARE": "go",
}
replay := runCgiTest(t, h, "GET /test.go?foo=bar&a=b HTTP/1.0\nHost: example.com\n\n", expectedMap)

if expected, got := "text/plain; charset=utf-8", replay.Header().Get("Content-Type"); got != expected {
t.Errorf("got a Content-Type of %q; expected %q", got, expected)
}
if expected, got := "X-Test-Value", replay.Header().Get("X-Test-Header"); got != expected {
t.Errorf("got a X-Test-Header of %q; expected %q", got, expected)
}
}

type customWriterRecorder struct {
w io.Writer
*httptest.ResponseRecorder
Expand Down Expand Up @@ -48,6 +91,109 @@ func (w *limitWriter) Write(p []byte) (n int, err error) {
return
}

// If there's an error copying the child's output to the parent, test
// that we kill the child.
func TestKillChildAfterCopyError(t *testing.T) {
testenv.MustHaveExec(t)

h := &Handler{
Path: os.Args[0],
Root: "/test.go",
Args: []string{"-test.run=TestBeChildCGIProcess"},
}
req, _ := http.NewRequest("GET", "http://example.com/test.cgi?write-forever=1", nil)
rec := httptest.NewRecorder()
var out bytes.Buffer
const writeLen = 50 << 10
rw := &customWriterRecorder{&limitWriter{&out, writeLen}, rec}

h.ServeHTTP(rw, req)
if out.Len() != writeLen || out.Bytes()[0] != 'a' {
t.Errorf("unexpected output: %q", out.Bytes())
}
}

// Test that a child handler writing only headers works.
// golang.org/issue/7196
func TestChildOnlyHeaders(t *testing.T) {
testenv.MustHaveExec(t)

h := &Handler{
Path: os.Args[0],
Root: "/test.go",
Args: []string{"-test.run=TestBeChildCGIProcess"},
}
expectedMap := map[string]string{
"_body": "",
}
replay := runCgiTest(t, h, "GET /test.go?no-body=1 HTTP/1.0\nHost: example.com\n\n", expectedMap)
if expected, got := "X-Test-Value", replay.Header().Get("X-Test-Header"); got != expected {
t.Errorf("got a X-Test-Header of %q; expected %q", got, expected)
}
}

// Test that a child handler does not receive a nil Request Body.
// golang.org/issue/39190
func TestNilRequestBody(t *testing.T) {
testenv.MustHaveExec(t)

h := &Handler{
Path: os.Args[0],
Root: "/test.go",
Args: []string{"-test.run=TestBeChildCGIProcess"},
}
expectedMap := map[string]string{
"nil-request-body": "false",
}
_ = runCgiTest(t, h, "POST /test.go?nil-request-body=1 HTTP/1.0\nHost: example.com\n\n", expectedMap)
_ = runCgiTest(t, h, "POST /test.go?nil-request-body=1 HTTP/1.0\nHost: example.com\nContent-Length: 0\n\n", expectedMap)
}

func TestChildContentType(t *testing.T) {
testenv.MustHaveExec(t)

h := &Handler{
Path: os.Args[0],
Root: "/test.go",
Args: []string{"-test.run=TestBeChildCGIProcess"},
}
var tests = []struct {
name string
body string
wantCT string
}{
{
name: "no body",
wantCT: "text/plain; charset=utf-8",
},
{
name: "html",
body: "<html><head><title>test page</title></head><body>This is a body</body></html>",
wantCT: "text/html; charset=utf-8",
},
{
name: "text",
body: strings.Repeat("gopher", 86),
wantCT: "text/plain; charset=utf-8",
},
{
name: "jpg",
body: "\xFF\xD8\xFF" + strings.Repeat("B", 1024),
wantCT: "image/jpeg",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
expectedMap := map[string]string{"_body": tt.body}
req := fmt.Sprintf("GET /test.go?exact-body=%s HTTP/1.0\nHost: example.com\n\n", url.QueryEscape(tt.body))
replay := runCgiTest(t, h, req, expectedMap)
if got := replay.Header().Get("Content-Type"); got != tt.wantCT {
t.Errorf("got a Content-Type of %q; expected it to start with %q", got, tt.wantCT)
}
})
}
}

// golang.org/issue/7198
func Test500WithNoHeaders(t *testing.T) { want500Test(t, "/immediate-disconnect") }
func Test500WithNoContentType(t *testing.T) { want500Test(t, "/no-content-type") }
Expand Down
15 changes: 13 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"net"
"net/url"
"reflect"
"runtime"
"strconv"
"strings"
"sync"
Expand All @@ -26,8 +27,9 @@ import (
"time"

. "github.com/ooni/oohttp"
"github.com/ooni/oohttp/cookiejar"
"github.com/ooni/oohttp/httptest"
cookiejar "github.com/ooni/oohttp/cookiejar"
httptest "github.com/ooni/oohttp/httptest"
testenv "github.com/ooni/oohttp/internal/testenv"
)

var robotsTxtHandler = HandlerFunc(func(w ResponseWriter, r *Request) {
Expand Down Expand Up @@ -1237,6 +1239,9 @@ func testClientTimeout(t *testing.T, mode testMode) {
t.Logf("timeout before response received")
continue
}
if runtime.GOOS == "windows" && strings.HasPrefix(runtime.GOARCH, "arm") {
testenv.SkipFlaky(t, 43120)
}
t.Fatal(err)
}

Expand All @@ -1260,6 +1265,9 @@ func testClientTimeout(t *testing.T, mode testMode) {
t.Errorf("net.Error.Timeout = false; want true")
}
if got := ne.Error(); !strings.Contains(got, "(Client.Timeout") {
if runtime.GOOS == "windows" && strings.HasPrefix(runtime.GOARCH, "arm") {
testenv.SkipFlaky(t, 43120)
}
t.Errorf("error string = %q; missing timeout substring", got)
}

Expand Down Expand Up @@ -1300,6 +1308,9 @@ func testClientTimeout_Headers(t *testing.T, mode testMode) {
t.Error("net.Error.Timeout = false; want true")
}
if got := ne.Error(); !strings.Contains(got, "Client.Timeout exceeded") {
if runtime.GOOS == "windows" && strings.HasPrefix(runtime.GOARCH, "arm") {
testenv.SkipFlaky(t, 43120)
}
t.Errorf("error string = %q; missing timeout substring", got)
}
}
Expand Down
6 changes: 3 additions & 3 deletions clientserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ import (
"time"

. "github.com/ooni/oohttp"
"github.com/ooni/oohttp/httptest"
"github.com/ooni/oohttp/httptrace"
"github.com/ooni/oohttp/httputil"
httptest "github.com/ooni/oohttp/httptest"
httptrace "github.com/ooni/oohttp/httptrace"
httputil "github.com/ooni/oohttp/httputil"
)

type testMode string
Expand Down
2 changes: 1 addition & 1 deletion cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"strings"
"time"

"github.com/ooni/oohttp/internal/ascii"
ascii "github.com/ooni/oohttp/internal/ascii"
)

// A Cookie represents an HTTP cookie as sent in the Set-Cookie header of an
Expand Down
6 changes: 3 additions & 3 deletions cookiejar/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"log"
"net/url"

"github.com/ooni/oohttp"
"github.com/ooni/oohttp/cookiejar"
"github.com/ooni/oohttp/httptest"
http "github.com/ooni/oohttp"
cookiejar "github.com/ooni/oohttp/cookiejar"
httptest "github.com/ooni/oohttp/httptest"
)

func ExampleNew() {
Expand Down
4 changes: 2 additions & 2 deletions cookiejar/jar.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"sync"
"time"

"github.com/ooni/oohttp"
"github.com/ooni/oohttp/internal/ascii"
http "github.com/ooni/oohttp"
ascii "github.com/ooni/oohttp/internal/ascii"
)

// PublicSuffixList provides the public suffix of a domain. For example:
Expand Down
2 changes: 1 addition & 1 deletion cookiejar/jar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"testing"
"time"

"github.com/ooni/oohttp"
http "github.com/ooni/oohttp"
)

// tNow is the synthetic current time used as now during testing.
Expand Down
2 changes: 1 addition & 1 deletion cookiejar/punycode.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
"unicode/utf8"

"github.com/ooni/oohttp/internal/ascii"
ascii "github.com/ooni/oohttp/internal/ascii"
)

// These parameter values are specified in section 5.
Expand Down
2 changes: 1 addition & 1 deletion example_filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"log"
"strings"

"github.com/ooni/oohttp"
http "github.com/ooni/oohttp"
)

// containsDotFile reports whether name contains a path element starting with a period.
Expand Down
2 changes: 1 addition & 1 deletion example_handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"log"
"sync"

"github.com/ooni/oohttp"
http "github.com/ooni/oohttp"
)

type countHandler struct {
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"os"
"os/signal"

"github.com/ooni/oohttp"
http "github.com/ooni/oohttp"
)

func ExampleHijacker() {
Expand Down
4 changes: 2 additions & 2 deletions fcgi/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"strings"
"time"

"github.com/ooni/oohttp"
"github.com/ooni/oohttp/cgi"
http "github.com/ooni/oohttp"
cgi "github.com/ooni/oohttp/cgi"
)

// request holds the state for an in-progress request. As soon as it's complete,
Expand Down
2 changes: 1 addition & 1 deletion fcgi/fcgi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"testing"
"time"

"github.com/ooni/oohttp"
http "github.com/ooni/oohttp"
)

var sizeTests = []struct {
Expand Down
2 changes: 1 addition & 1 deletion fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"strings"
"time"

"github.com/ooni/oohttp/internal/safefilepath"
safefilepath "github.com/ooni/oohttp/internal/safefilepath"
)

// A Dir implements FileSystem using the native file system restricted to a
Expand Down
2 changes: 1 addition & 1 deletion fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"time"

. "github.com/ooni/oohttp"
"github.com/ooni/oohttp/httptest"
httptest "github.com/ooni/oohttp/httptest"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion h2_bundle.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0628efa

Please sign in to comment.