summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-12-20 14:56:47 +0300
committerWolfy-J <[email protected]>2018-12-20 14:56:47 +0300
commitc63dd02f3ea0dfa326cfd20f259b235aca3fba1b (patch)
tree4feef09cc17c98d9acd295268737bb67cff0498d
parentea60c3e1eba96cd140e4c89b710de3a98f523f43 (diff)
added tests for USER_AGENT
-rw-r--r--service/http/handler_test.go93
-rw-r--r--src/PSR7Client.php6
-rw-r--r--tests/http/user-agent.php10
3 files changed, 108 insertions, 1 deletions
diff --git a/service/http/handler_test.go b/service/http/handler_test.go
index e864b86e..1750bf43 100644
--- a/service/http/handler_test.go
+++ b/service/http/handler_test.go
@@ -167,6 +167,99 @@ func TestServer_Headers(t *testing.T) {
assert.Equal(t, "SAMPLE", string(b))
}
+func TestServer_Empty_User_Agent(t *testing.T) {
+ st := &Handler{
+ cfg: &Config{
+ MaxRequest: 1024,
+ Uploads: &UploadsConfig{
+ Dir: os.TempDir(),
+ Forbid: []string{},
+ },
+ },
+ rr: roadrunner.NewServer(&roadrunner.ServerConfig{
+ Command: "php ../../tests/http/client.php user-agent pipes",
+ Relay: "pipes",
+ Pool: &roadrunner.Config{
+ NumWorkers: 1,
+ AllocateTimeout: 10000000,
+ DestroyTimeout: 10000000,
+ },
+ }),
+ }
+
+ assert.NoError(t, st.rr.Start())
+ defer st.rr.Stop()
+
+ hs := &http.Server{Addr: ":8088", Handler: st}
+ defer hs.Shutdown(context.Background())
+
+ go func() { hs.ListenAndServe() }()
+ time.Sleep(time.Millisecond * 10)
+
+ req, err := http.NewRequest("GET", "http://localhost:8088?hello=world", nil)
+ assert.NoError(t, err)
+
+ req.Header.Add("user-agent", "")
+
+ r, err := http.DefaultClient.Do(req)
+ assert.NoError(t, err)
+ defer r.Body.Close()
+
+ b, err := ioutil.ReadAll(r.Body)
+ assert.NoError(t, err)
+
+ assert.NoError(t, err)
+ assert.Equal(t, 200, r.StatusCode)
+ assert.Equal(t, "", string(b))
+}
+
+
+func TestServer_User_Agent(t *testing.T) {
+ st := &Handler{
+ cfg: &Config{
+ MaxRequest: 1024,
+ Uploads: &UploadsConfig{
+ Dir: os.TempDir(),
+ Forbid: []string{},
+ },
+ },
+ rr: roadrunner.NewServer(&roadrunner.ServerConfig{
+ Command: "php ../../tests/http/client.php user-agent pipes",
+ Relay: "pipes",
+ Pool: &roadrunner.Config{
+ NumWorkers: 1,
+ AllocateTimeout: 10000000,
+ DestroyTimeout: 10000000,
+ },
+ }),
+ }
+
+ assert.NoError(t, st.rr.Start())
+ defer st.rr.Stop()
+
+ hs := &http.Server{Addr: ":8088", Handler: st}
+ defer hs.Shutdown(context.Background())
+
+ go func() { hs.ListenAndServe() }()
+ time.Sleep(time.Millisecond * 10)
+
+ req, err := http.NewRequest("GET", "http://localhost:8088?hello=world", nil)
+ assert.NoError(t, err)
+
+ req.Header.Add("User-Agent", "go-agent")
+
+ r, err := http.DefaultClient.Do(req)
+ assert.NoError(t, err)
+ defer r.Body.Close()
+
+ b, err := ioutil.ReadAll(r.Body)
+ assert.NoError(t, err)
+
+ assert.NoError(t, err)
+ assert.Equal(t, 200, r.StatusCode)
+ assert.Equal(t, "go-agent", string(b))
+}
+
func TestServer_Cookies(t *testing.T) {
st := &Handler{
cfg: &Config{
diff --git a/src/PSR7Client.php b/src/PSR7Client.php
index 129cb118..0b148884 100644
--- a/src/PSR7Client.php
+++ b/src/PSR7Client.php
@@ -144,7 +144,11 @@ class PSR7Client
$server['REQUEST_TIME_FLOAT'] = microtime(true);
$server['REMOTE_ADDR'] = $ctx['attributes']['ipAddress'] ?? $ctx['remoteAddr'] ?? '127.0.0.1';
$server['REMOTE_ADDR'] = $ctx['attributes']['ipAddress'] ?? $ctx['remoteAddr'] ?? '127.0.0.1';
- $server['HTTP_USER_AGENT'] = $ctx['headers']['User-Agent'][0] ?? '';
+
+ $server['HTTP_USER_AGENT'] = '';
+ if (isset($ctx['headers']['User-Agent'][0])) {
+ $server['HTTP_USER_AGENT'] = $ctx['headers']['User-Agent'][0];
+ }
return $server;
}
diff --git a/tests/http/user-agent.php b/tests/http/user-agent.php
new file mode 100644
index 00000000..029f49e9
--- /dev/null
+++ b/tests/http/user-agent.php
@@ -0,0 +1,10 @@
+<?php
+
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+
+function handleRequest(ServerRequestInterface $req, ResponseInterface $resp): ResponseInterface
+{
+ $resp->getBody()->write($_SERVER['HTTP_USER_AGENT']);
+ return $resp;
+} \ No newline at end of file