加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何在Perl中模拟Web服务器?

发布时间:2020-12-16 06:20:05 所属栏目:大数据 来源:网络整理
导读:我们的Web应用程序与几个我们无法影响的Web服务一起工作.在每个工作流程(使用Selenium测试)之后,发生对Web服务的挂钩调用.我想嘲笑那个服务器.理想情况下,我想要一个HTTP服务器对象,我可以随意启动和终止,以及一个URL调度程序,它会在调用时调用我的测试中的
我们的Web应用程序与几个我们无法影响的Web服务一起工作.在每个工作流程(使用Selenium测试)之后,发生对Web服务的挂钩调用.我想嘲笑那个服务器.理想情况下,我想要一个HTTP服务器对象,我可以随意启动和终止,以及一个URL调度程序,它会在调用时调用我的测试中的某些子程序.

到目前为止,我已经找到HTTP :: Server :: Simple和HTTP :: Server :: Brick,我发现后者更具吸引力.你有其他内幕消息吗?

解决方法

我使用HTTP :: Daemon和Template :: Toolkit的组合来做到这一点.

package Test::WebService;

use HTTP::Daemon;
use HTTP::Response;
use IO::File;
use Template;

our $PID  = $$;

END { __PACKAGE__->StopWeb(); }

sub StartWeb : method {

    my $self    = shift;
    my $port    = shift;
    my %actions = $_[0] && ref($_[0]) eq 'HASH' ? %{ $_[0] } : @_ %2 ? () : @_;

    # Ignore CHLD
    local $SIG{CHLD} = 'IGNORE';

    # Fork
    my $pid = fork();

    if ( $pid == 0 )
    {
        # Create pid file
        _createPid( "/tmp/httpd.pid" );

        # Create server
        eval
        {
            # Create socket
            my $d = HTTP::Daemon->new
            (
                Listen      => 1,LocalPort   => $port,Reuse       => 1,) || die "Failed to bind socket";

            # Listen for connections
            while ( my $c = $d->accept )
            {
                # Process requests
                while ( my $r = $c->get_request() )
                {               
                    if ( defined( my $tmpl = $actions{ $r->uri()->path() } ) )
                    {
                        eval
                        {
                            # Create template object
                            my $tt = Template->new( {ABSOLUTE => 1 } );

                            # Create response
                            my $rs = HTTP::Response->new('200');

                            # Process template
                            $tt->process
                            (
                                $tmpl,$r->uri()->query_form_hash(),sub { $rs->content( shift ) }
                            );

                            # Send response
                            $c->send_response( $rs );
                        };

                        if ($@)
                        {
                            $c->send_error('500',$@ );
                        }

                    }
                    else
                    {
                        $c->send_error('404','No Template Found');
                    }
                }
            }
        };

        if ($@)
        {
            # Remove pid file
            unlink "/tmp/httpd.pid";

            # die
            die $@;
        }

        # Exit nicely
        exit(0);
    }

    # Wait up to 5 seconds for server to start;
    die "Failed to start http server" unless _waitpid( 5,"/tmp/httpd.pid" );   

}

sub StopWeb {

    # Only cleanup parent process.
    if ( $PID && $PID == $$)
    {
        if ( my $fh = IO::File->new( "/tmp/httpd.pid",'r') )
        {
            # Get pid.
            my $pid;
            $fh->read( $pid,16384 ); 
            $pid =~ s/D//g;

            # Kill server
            kill 4,$pid if $pid;
        }
    }
}

sub _createPid {

    my $fh = IO::File->new( shift,'w') || die "Couldn't create pid";
    $fh->print("$$");
    $fh->close(); 

    return;
}

sub _waitpid {

    my $secs = shift || 5;
    my $file = shift || die "Missing pid file";

    for( my $i=0; $i

然后可以将测试代码写成:

#!/usr/bin/perl

use Test::More tests => 1;
use Test::WebService;

use MyApp;

Test::WebService->StartWeb( '8088','/webservice/method' => 'my.tmpl' );

ok ( MyApp->methodThatCallsWebService(),'yay!' );

1;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读