<commit_before>// HttpRequestTest.java

/**
*    Copyright (C) 2008 10gen Inc.
*
*    This program is free software: you can redistribute it and/or  modify
*    it under the terms of the GNU Affero General Public License, version 3,
*    as published by the Free Software Foundation.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Affero General Public License for more details.
*
*    You should have received a copy of the GNU Affero General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package ed.net.httpserver;

import org.testng.annotations.Test;

import ed.*;

public class HttpRequestTest extends TestCase {

    @Test(groups = {"basic"})
    public static void testGetParams(){
        HttpRequest r = HttpRequest.getDummy( "/crazy/me?a=1&b=2&b=3" );
        assertEquals( "1" , r.getParameter( "a" ) );
        assertEquals( "1" , r.param( "a" ) );
        assertEquals( "1" , r.getURLParameter( "a" ) );
        assertEquals( "1" , r.getURLParameter( "a" , "asdsad" ) );
        assertEquals( "Z" , r.getURLParameter( "c" , "Z" ) );
        assertNull( r.getPostParameter( "a" ) );

        assertEquals( 2 , r.getParameters( "b" ).size() );
        assertEquals( "2" , r.getParameter( "b" ) );
        assertEquals( "2" , r.getParameters( "b" ).get(0).toString() );
        assertEquals( "3" , r.getParameters( "b" ).get(1).toString() );
        assertNull( r.getParameters( "b" ).get(2) );

        assertEquals( 2 , r.getParameterNamesArray().size() );
    }

    @Test(groups = {"basic"})
    public static void testPostParams(){
        HttpRequest r = HttpRequest.getDummy( "/crazy/me?a=1&b=2&b=3&c=Z" );

        r._addParm( "c" , "Y" , true );

        assertEquals( "Y" , r.getPostParameter( "c" ) );
        assertEquals( "Y" , r.getParameter( "c" ) );
        assertEquals( "Z" , r.getURLParameter( "c" ) );

        assertEquals( 3 , r.getParameterNamesArray().size() );
        assertEquals( 1 , r.getPostParameterNames().size() );
        assertEquals( 3 , r.getURLParameterNames().size() );
    }

    @Test(groups = {"basic"})
    public static void testJSInterface(){
        HttpRequest r = HttpRequest.getDummy( "/crazy/me?a=2" );
        assertEquals( "2" , r.get( "a" ).toString() );
        r.set( "a" , "3" );
        assertEquals( "3" , r.get( "a" ).toString() );
    }


    @Test(groups = {"basic"})
    public static void testPaths(){
        HttpRequest r = HttpRequest.getDummy( "/crazy/me?a=2" );
        assertEquals( "/crazy/" , r.getDirectory() );

        r = HttpRequest.getDummy( "/crazy/me/?a=2" );
        assertEquals( "/crazy/me/" , r.getDirectory() );

        r = HttpRequest.getDummy( "/crazy?a=2" );
        assertEquals( "/" , r.getDirectory() );

    }

    @Test(groups = {"basic"})
    public static void testRandom(){
        // yes, this shouldn't be here.

        HttpRequest r = HttpRequest.getDummy( "/?f=data" , "Referer: http://blah.com/asda" );
        assertEquals( "http://blah.com/asda" , r.getReferer() );
        assertEquals( "/asda" , r.getRefererNoHost() );

        r = HttpRequest.getDummy( "/?f=data" , "Referer: /asda" );
        assertEquals( "/asda" , r.getHeader( "Referer" ) );
        assertEquals( "/asda" , r.getHeadersObject().get( "Referer" ).toString() );
        assertEquals( "/asda" , r.getReferer() );
        assertEquals( "/asda" , r.getRefererNoHost() );

    }

    @Test(groups = {"basic"})
    public static void testMonitorGetFilter(){
        // yes, this shouldn't be here.

        HttpRequest r = HttpRequest.getDummy( "/?f=data" );
        assertEquals( "data" , HttpMonitor.ThreadMonitor.getFilter( r ) );

        r = HttpRequest.getDummy( "/?a=data" );<commit_msg>test for getRefererNoHost<commit_after>// HttpRequestTest.java

/**
*    Copyright (C) 2008 10gen Inc.
*
*    This program is free software: you can redistribute it and/or  modify
*    it under the terms of the GNU Affero General Public License, version 3,
*    as published by the Free Software Foundation.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Affero General Public License for more details.
*
*    You should have received a copy of the GNU Affero General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package ed.net.httpserver;

import org.testng.annotations.Test;

import ed.*;

public class HttpRequestTest extends TestCase {

    @Test(groups = {"basic"})
    public static void testGetParams(){
        HttpRequest r = HttpRequest.getDummy( "/crazy/me?a=1&b=2&b=3" );
        assertEquals( "1" , r.getParameter( "a" ) );
        assertEquals( "1" , r.param( "a" ) );
        assertEquals( "1" , r.getURLParameter( "a" ) );
        assertEquals( "1" , r.getURLParameter( "a" , "asdsad" ) );
        assertEquals( "Z" , r.getURLParameter( "c" , "Z" ) );
        assertNull( r.getPostParameter( "a" ) );

        assertEquals( 2 , r.getParameters( "b" ).size() );
        assertEquals( "2" , r.getParameter( "b" ) );
        assertEquals( "2" , r.getParameters( "b" ).get(0).toString() );
        assertEquals( "3" , r.getParameters( "b" ).get(1).toString() );
        assertNull( r.getParameters( "b" ).get(2) );

        assertEquals( 2 , r.getParameterNamesArray().size() );
    }

    @Test(groups = {"basic"})
    public static void testPostParams(){
        HttpRequest r = HttpRequest.getDummy( "/crazy/me?a=1&b=2&b=3&c=Z" );

        r._addParm( "c" , "Y" , true );

        assertEquals( "Y" , r.getPostParameter( "c" ) );
        assertEquals( "Y" , r.getParameter( "c" ) );
        assertEquals( "Z" , r.getURLParameter( "c" ) );

        assertEquals( 3 , r.getParameterNamesArray().size() );
        assertEquals( 1 , r.getPostParameterNames().size() );
        assertEquals( 3 , r.getURLParameterNames().size() );
    }

    @Test(groups = {"basic"})
    public static void testJSInterface(){
        HttpRequest r = HttpRequest.getDummy( "/crazy/me?a=2" );
        assertEquals( "2" , r.get( "a" ).toString() );
        r.set( "a" , "3" );
        assertEquals( "3" , r.get( "a" ).toString() );
    }


    @Test(groups = {"basic"})
    public static void testPaths(){
        HttpRequest r = HttpRequest.getDummy( "/crazy/me?a=2" );
        assertEquals( "/crazy/" , r.getDirectory() );

        r = HttpRequest.getDummy( "/crazy/me/?a=2" );
        assertEquals( "/crazy/me/" , r.getDirectory() );

        r = HttpRequest.getDummy( "/crazy?a=2" );
        assertEquals( "/" , r.getDirectory() );

    }

    @Test(groups = {"basic"})
    public static void testRandom(){
        // yes, this shouldn't be here.

        HttpRequest r = HttpRequest.getDummy( "/?f=data" , "Referer: http://blah.com/asda" );
        assertEquals( "http://blah.com/asda" , r.getReferer() );
        assertEquals( "/asda" , r.getRefererNoHost() );

        r = HttpRequest.getDummy( "/?f=data" , "Referer: /asda" );
        assertEquals( "/asda" , r.getHeader( "Referer" ) );
        assertEquals( "/asda" , r.getHeadersObject().get( "Referer" ).toString() );
        assertEquals( "/asda" , r.getReferer() );
        assertEquals( "/asda" , r.getRefererNoHost() );

    }

    @Test(groups = {"basic"})
    public static void testRefererNoHost () {
        HttpRequest r;
        r = HttpRequest.getDummy("/?f=data", "Referer: /blah/huh");
        assertEquals("/blah/huh", r.getRefererNoHost());

        r = HttpRequest.getDummy("/?f=data", "Referer: http://www.10gen.com/blah/huh");
        assertEquals("/blah/huh", r.getRefererNoHost());

        r = HttpRequest.getDummy("/?f=data", "Referer: http://www.10gen.com/");
        assertEquals("/", r.getRefererNoHost());

        r = HttpRequest.getDummy("/?f=data", "Referer: http://www.10gen.com");
        assertEquals("/", r.getRefererNoHost());
    }

    @Test(groups = {"basic"})
    public static void testMonitorGetFilter(){
        // yes, this shouldn't be here.

        HttpRequest r = HttpRequest.getDummy( "/?f=data" );
        assertEquals( "data" , HttpMonitor.ThreadMonitor.getFilter( r ) );

        r = HttpRequest.getDummy( "/?a=data" );
{'fraction_non_alphanumeric': 0.1683673469387755, 'fraction_numerical': 0.009017560512577124, 'mean_word_length': 2.732949512843224, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 5, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4884695', 'n_tokens_mistral': 2789, 'n_tokens_neox': 2700, 'n_words': 668}