github.com/symfony-cli/symfony-cli@v0.0.0-20240514161054-ece2df437dfa/local/php/envs_test.go (about)

     1  /*
     2   * Copyright (c) 2021-present Fabien Potencier <fabien@symfony.com>
     3   *
     4   * This file is part of Symfony CLI project
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU Affero General Public License as
     8   * published by the Free Software Foundation, either version 3 of the
     9   * License, or (at your option) any later version.
    10   *
    11   * This program is distributed in the hope that it will be useful,
    12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    14   * GNU Affero General Public License for more details.
    15   *
    16   * You should have received a copy of the GNU Affero General Public License
    17   * along with this program. If not, see <http://www.gnu.org/licenses/>.
    18   */
    19  
    20  package php
    21  
    22  import (
    23  	"net/http"
    24  	"testing"
    25  
    26  	. "gopkg.in/check.v1"
    27  )
    28  
    29  func Test(t *testing.T) { TestingT(t) }
    30  
    31  type PHPFPMSuite struct{}
    32  
    33  var _ = Suite(&PHPFPMSuite{})
    34  
    35  func (s *PHPFPMSuite) TestGenerateEnv(c *C) {
    36  	testdataDir := "testdata"
    37  	tests := []struct {
    38  		uri      string
    39  		passthru string
    40  		expected map[string]string
    41  	}{
    42  		{
    43  			passthru: "/index.php",
    44  			uri:      "/",
    45  			expected: map[string]string{
    46  				"PATH_INFO":       "/",
    47  				"REQUEST_URI":     "/",
    48  				"QUERY_STRING":    "",
    49  				"SCRIPT_FILENAME": testdataDir + "/public/index.php",
    50  				"SCRIPT_NAME":     "/index.php",
    51  			},
    52  		},
    53  		{
    54  			passthru: "/index.php",
    55  			uri:      "/?foo=bar",
    56  			expected: map[string]string{
    57  				"PATH_INFO":       "/",
    58  				"REQUEST_URI":     "/?foo=bar",
    59  				"QUERY_STRING":    "foo=bar",
    60  				"SCRIPT_FILENAME": testdataDir + "/public/index.php",
    61  				"SCRIPT_NAME":     "/index.php",
    62  			},
    63  		},
    64  		{
    65  			passthru: "/index.php",
    66  			uri:      "/index.php",
    67  			expected: map[string]string{
    68  				"PATH_INFO":       "",
    69  				"REQUEST_URI":     "/index.php",
    70  				"QUERY_STRING":    "",
    71  				"SCRIPT_FILENAME": testdataDir + "/public/index.php",
    72  				"SCRIPT_NAME":     "/index.php",
    73  			},
    74  		},
    75  		{
    76  			passthru: "/index.php",
    77  			uri:      "/index.php/foo",
    78  			expected: map[string]string{
    79  				"PATH_INFO":       "/foo",
    80  				"REQUEST_URI":     "/index.php/foo",
    81  				"QUERY_STRING":    "",
    82  				"SCRIPT_FILENAME": testdataDir + "/public/index.php",
    83  				"SCRIPT_NAME":     "/index.php",
    84  			},
    85  		},
    86  		{
    87  			passthru: "/app.PHP",
    88  			uri:      "/app.PHP/foo",
    89  			expected: map[string]string{
    90  				"PATH_INFO":       "/foo",
    91  				"REQUEST_URI":     "/app.PHP/foo",
    92  				"QUERY_STRING":    "",
    93  				"SCRIPT_FILENAME": testdataDir + "/public/app.PHP",
    94  				"SCRIPT_NAME":     "/app.PHP",
    95  			},
    96  		},
    97  		{
    98  			passthru: "/index.php",
    99  			uri:      "/index.php/foo?foo=bar",
   100  			expected: map[string]string{
   101  				"PATH_INFO":       "/foo",
   102  				"REQUEST_URI":     "/index.php/foo?foo=bar",
   103  				"QUERY_STRING":    "foo=bar",
   104  				"SCRIPT_FILENAME": testdataDir + "/public/index.php",
   105  				"SCRIPT_NAME":     "/index.php",
   106  			},
   107  		},
   108  		{
   109  			passthru: "/index.php",
   110  			uri:      "/foo",
   111  			expected: map[string]string{
   112  				"PATH_INFO":       "/foo",
   113  				"REQUEST_URI":     "/foo",
   114  				"QUERY_STRING":    "",
   115  				"SCRIPT_FILENAME": testdataDir + "/public/index.php",
   116  				"SCRIPT_NAME":     "/index.php",
   117  			},
   118  		},
   119  		{
   120  			passthru: "/index.php",
   121  			uri:      "/update.php",
   122  			expected: map[string]string{
   123  				"PATH_INFO":       "",
   124  				"REQUEST_URI":     "/update.php",
   125  				"QUERY_STRING":    "",
   126  				"SCRIPT_FILENAME": testdataDir + "/public/update.php",
   127  				"SCRIPT_NAME":     "/update.php",
   128  			},
   129  		},
   130  		{
   131  			passthru: "/index.php",
   132  			uri:      "/js/whitelist.php",
   133  			expected: map[string]string{
   134  				"PATH_INFO":       "",
   135  				"REQUEST_URI":     "/js/whitelist.php",
   136  				"QUERY_STRING":    "",
   137  				"SCRIPT_FILENAME": testdataDir + "/public/js/whitelist.php",
   138  				"SCRIPT_NAME":     "/js/whitelist.php",
   139  			},
   140  		},
   141  		{
   142  			passthru: "/index.php",
   143  			uri:      "/update.php",
   144  			expected: map[string]string{
   145  				"PATH_INFO":       "",
   146  				"REQUEST_URI":     "/update.php",
   147  				"QUERY_STRING":    "",
   148  				"SCRIPT_FILENAME": testdataDir + "/public/update.php",
   149  				"SCRIPT_NAME":     "/update.php",
   150  			},
   151  		},
   152  		{
   153  			passthru: "/index.php",
   154  			uri:      "/unknown.php",
   155  			expected: map[string]string{
   156  				"PATH_INFO":       "/unknown.php",
   157  				"REQUEST_URI":     "/unknown.php",
   158  				"QUERY_STRING":    "",
   159  				"SCRIPT_FILENAME": testdataDir + "/public/index.php",
   160  				"SCRIPT_NAME":     "/index.php",
   161  			},
   162  		},
   163  		{
   164  			passthru: "/index.php",
   165  			uri:      "/unknown.php/foo",
   166  			expected: map[string]string{
   167  				"PATH_INFO":       "/unknown.php/foo",
   168  				"REQUEST_URI":     "/unknown.php/foo",
   169  				"QUERY_STRING":    "",
   170  				"SCRIPT_FILENAME": testdataDir + "/public/index.php",
   171  				"SCRIPT_NAME":     "/index.php",
   172  			},
   173  		},
   174  		{
   175  			passthru: "/index.php",
   176  			uri:      "/unknown.PHP/foo",
   177  			expected: map[string]string{
   178  				"PATH_INFO":       "/unknown.PHP/foo",
   179  				"REQUEST_URI":     "/unknown.PHP/foo",
   180  				"QUERY_STRING":    "",
   181  				"SCRIPT_FILENAME": testdataDir + "/public/index.php",
   182  				"SCRIPT_NAME":     "/index.php",
   183  			},
   184  		},
   185  	}
   186  	for _, test := range tests {
   187  		process := &Server{
   188  			projectDir:   testdataDir,
   189  			documentRoot: testdataDir + "/public/",
   190  			passthru:     test.passthru,
   191  		}
   192  		req, err := http.NewRequest("GET", test.uri, nil)
   193  		c.Assert(err, IsNil)
   194  
   195  		req.RequestURI = test.uri
   196  		env := process.generateEnv(req)
   197  		for k, v := range test.expected {
   198  			vv, ok := env[k]
   199  			c.Assert(ok, Equals, true)
   200  			c.Assert(vv, DeepEquals, v)
   201  		}
   202  	}
   203  }