阅读背景:

如何使用npm superagent登录cloud9

来源:互联网 

Objective

Login into cloud9 using npm superagent so I can perform tests.

使用npm superagent登录cloud9,这样我就可以进行测试了。

Background

I am making a NodeJS app using npm mocha with superagent to conduct some tests.

我使用npm mocha和superagent进行NodeJS应用程序进行一些测试。

Npm mocha works perfectly, the problem is when I try to use superagent to test the contents of my webpages.

Npm mocha工作得很好,问题是当我尝试使用superagent来测试我的网页内容时。

Error

Every time I try to test the content of one of my pages, I get the following error:

每次我尝试测试其中一个页面的内容时,都会收到以下错误:

  server
    1) prints out 'Hello, world' when user goes to /


  0 passing (1s)
  1 failing

  1) server prints out 'Hello, world' when user goes to /:

      Uncaught AssertionError: '<!DOCTYPE html>\n<html xmlns="https://www.w3.org/1999/xhtml">\n    <head profile="https://www.w3.org/2005/10/profile">\n        < == 'Hello, world'
      + expected - actual

      -<!DOCTYPE html>
      -<html xmlns="https://www.w3.org/1999/xhtml">
      -    <head profile="https://www.w3.org/2005/10/profile">
      -        <!--C9LOCAL_CODE_INJECT_PLACEHOLDER-(login-head)-DO_NOT_REMOVE-->
      -        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
      -        <title>Sign-in | Cloud9 IDE - Ajax.org</title>
      -        <meta name="description" content="Meet Cloud9, development-as-a-service for Javascripters and other developers"/>
      -        <meta name="keywords" content=""/>
      -
      -        <link rel="icon" type="image/gif" href="/go.html?url=https://cdn.c9.io/nc-3.1.3023-61dfb026/static/homepage/favicon.ico" />
      -
      -        <!--C9LOCAL_CODE_INJECT_PLACEHOLDER-(login-loadedDetectionScript)-DO_NOT_REMOVE-->
      -        <link href="/go.html?url=https://cdn.c9.io/nc-3.1.3023-61dfb026/static/oldclient/homepage/style/signin.css" rel="stylesheet" type="text/css" />
      -        <script type="text/javascript" src="https://cdn.c9.io/nc-3.1.3023-61dfb026/static/oldclient/homepage/js/prefixfree.min.js"></script>
      -    </head>
      -    <body class="">
      -        <script type="text/javascript">
      -            // ClickTale Top part
      -            var WRInitTime=(new Date()).getTime();
      -            // ClickTale end of Top part
      -        </script>
      -        <div id="header">
      -            <a class="logo" href="/go.html?url=/"></a>
      -            <div class="social">
      -                <div class="socialMediaBlok">
      -                    <a href="/go.html?url=https://twitter.com/share" class="twitter-share-button" data-url="https://c9.io/" data-text="Cloud9 IDE - Your code anywhere, anytime" data-count="horizontal" data-via="cloud9ide">Tweet</a>
      -                    <script type="text/javascript" src="//platform.twitter.com/widgets.js"></script>
      -                </div>
      -                <div class="socialMediaBlok">
      -                    <iframe src="//www.facebook.com/plugins/like.php?href=c9.io&amp;layout=button_count&amp;show_faces=false&amp;width=90&amp;action=like&amp;font=lucida+grande&amp;colorscheme=light&amp;height=21"
      -                      allowtransparency="true"
      -                      scrolling="no"
      -                      frameborder="0"
      -                      style="border:none; overflow:hidden;height:20px"></iframe>
      -                </div>
      -            </div>
      -        </div>
      -        <div class="headerdivider"></div>
      -        <div id="signin_window">
      -            <div id="signinViewport">
      -                <div class="pageContainer">
      -                    <div id="barForgetPass" class="page">
      -                        <form id="resetPasswordForm">
      -                            <div class="header">Reset password</div>
      -                            <div class="form-holder">
      -                                <div id="lbl_inpResetPassword" class="c9-label">
      -                                    <label for="inpResetPassword">Username or email address</label>
      -                                </div>
      -                                <div class="c9-textbox">
      -                                    <div class="c9-txt_fix">
      -                                        <input type="text" id="inpResetPassword" name="inpResetPassword" disabled2="disabled" />
      -                                    </div>
      -                                </div>
      -                            </div>
      -                        </form>
      -
      -                        <div class="fbox">
      -                            <div id="btnRPCancel" class="cancel-button button">Go back</div>
      -                            <div id="btnRP" class="action-button button">Reset my password</div>
      -                        </div>
      -                        <div class="label3">Your password will be reset and you will receive an email with a new password.</div>
      -                    </div>
      -                    <div id="barSignIn" class="page">
      -                        <form id="signinForm">
      -                            <div class="header">Please sign in</div>
      -                            <div class="form-holder">
      -                                <div id="lbl_inpUsernameEmail" class="c9-label">
      -                                    <label for="inpUsernameEmail">Username or email address</label>
      -                                </div>
      -                                <div id="txt_inpUsernameEmail" class="c9-textbox">
      -                                    <div class="c9-txt_fix">
      -                                        <input type="text" id="inpUsernameEmail" name="inpUsernameEmail" tabindex="1" />
      -                                    </div>
      -                                </div>
      -                                <div id="lbl_inpPassword" class="c9-label">
      -                                    <label for="inpPassword">Password</label>
      -                                    <a id="forgetlink" class="forgetlink">Forgot?</a>
      -                                </div>
      -                                <div id="txt_inpPassword" class="c9-textbox">
      -                                    <div class="c9-txt_fix">
      -                                        <input type="password" id="inpPassword" name="inpPassword" tabindex="2" />
      -                                    </div>
      -                                </div>
      -
      -                                <div class="fbox">
      -                                    <div id="cbRememberLogin" class="c9-checkbox" tabindex="3">
      -                                        <div class="check"></div><span>Remember my login</span>
      -                                    </div>
      -                                    <div id="btnSignIn" class="action-button button" tabindex="4"  accesskey="ENTER">Sign in</div>
      -                                </div>
      -                            </div>
      -                            <div id="barSignInStatusMsg" class="signinstatus-bar">
      -                                <div id="lblSignInHeader" class="errorboxContent"></div>
      -                                <div id="lblSignInStatus" class="errorboxContent"></div>
      -                            </div>
      -                        </form>
      -<!--                        <div id="btnLoginViaGitHub" class="c9-button btn-github" style="margin: 13px 0 0 12px;" tabindex="5"></div>-->
      -                        <div class="signin_options">
      -                            <div class='info'>Or sign in with:</div>
      -<!--                            <a href="/go.html?url=#" class="c9-button google-signin" tabindex="8"></a>
      -                            <a href="/go.html?url=#" class="c9-button twitter-signin" tabindex="7"></a>
      -                            <a href="/go.html?url=#" class="c9-button facebook-signin" tabindex="6"></a>-->
      -                            <a href="/go.html?url=#" class="c9-button github-signin"></a>
      -                            <a href="/go.html?url=#" class="c9-button bitbucket-signin"></a>
      -                        </div>
      -                    </div>
      -                    <div id="barActivationLink" class="page">
      -                            <form id="activationlinkForm">
      -                                <div class="header">Activation email</div>
      -                                <div class="form-holder">
      -                                    <div id="lbl_inpResetPassword" class="c9-label">
      -                                        <label for="inpResetPassword">Username or email address</label>
      -                                    </div>
      -                                    <div class="c9-textbox">
      -                                        <div class="c9-txt_fix">
      -                                            <input type="text" id="inpResendConfirmation" name="inpResendConfirmation" />
      -                                        </div>
      -                                    </div>
      -                                </div>
      -                            </form>
      -                            
      -                            <div class="fbox">
      -                                <div id="btnALCancel" class="cancel-button button">Go back</div>
      -                                <div id="btnAL" class="action-button button">Resend activation email</div>
      -                            </div>
      -                            <!--div style="clear: both"></div-->
      -                        <!--div class="divider" style="margin:40px 5px 0 5px;"></div>
      -                        <div class="label3" style="margin:21px 17px 0 17px;">To receive the registration email again please click the button above.</div-->
      -                    </div>
      -                </div>
      -            </div>
      -        </div>
      -        <div id="signup_window">
      -            <div class="no_account_yet"></div>
      -            <div class="signuplink-bar">
      -                <div class="bird"></div>
      -                <div class="content">
      -                    <a id="btnSignUpUrl" href="/go.html?url=/signup">SIGN UP</a> FOR YOUR ACCOUNT
      -                </div>
      -            </div>
      -        </div>
      -<!--        <div id="terms_of_use">
      -            <a href="/go.html?url=#">Terms of use</a> - &#169; 2011 - <a href="/go.html?url=/">Register here</a>
      -        </div>-->
      -        <div class="sign_up_now">
      -            <a id="resendactivation">Resend my activation email</a>
      -        </div>
      -        
      -
      -        <ul class="bottom_menu">
      -            <li><a href="/go.html?url=/" class="selected">Home</a></li>
      -            <li>|</li>
      -            <li>Talk to us at <a href="/go.html?url=https://twitter.com/cloud9ide/" target="_blank">Twitter</a> and <a href="/go.html?url=https://www.facebook.com/Cloud9IDE/" target="_blank">Facebook</a></li>
      -<!--            <li>|</li>-->
      -<!--            <li><a href="/go.html?url=#">Terms of use</a></li>-->
      -            <li>|</li>
      -            <li>Cloud9 IDE, Inc &#169; 2011</li>
      -        </ul>
      -
      -        <script type="text/javascript" src="https://cdn.c9.io/nc-3.1.3023-61dfb026/static/oldclient/homepage/js/jquery-1.5.1.min.js"></script>
      -        <script type="text/javascript" src="https://cdn.c9.io/nc-3.1.3023-61dfb026/static/oldclient/homepage/js/jquery.validate.min.js"></script>
      -        <script type="text/javascript" src="https://cdn.c9.io/nc-3.1.3023-61dfb026/static/oldclient/homepage/js/components.js"></script>
      -        <script type="text/javascript" src="https://cdn.c9.io/nc-3.1.3023-61dfb026/static/oldclient/homepage/js/code.js"></script>
      -        <script type="text/javascript" src="https://cdn.c9.io/nc-3.1.3023-61dfb026/static/oldclient/homepage/js/signin.js"></script>
      -        <!-- ClickTale Bottom part -->
      -        <div id="ClickTaleDiv" style="display: none;"></div>
      -        <script type='text/javascript'>
      -            document.write(unescape("%3Cscript%20src=\"" + (document.location.protocol == "https:" 
      -                ? "https://clicktale.pantherssl.com/" 
      -                : "https://s.clicktale.net/") + "WRc5.js\"%20type=\"text/javascript\"%3E%3C/script%3E"));
      -            </script>
      -        <script type="text/javascript">
      -            var ClickTaleSSL = 1;
      -            if (typeof ClickTale == "function")
      -                ClickTale(48230, 1, "www");
      -        </script>
      -        <!-- ClickTale end of Bottom part -->
      -    </body>
      -</html>
      +Hello, world

      at test2.js:23:20
      at Request.callback (node_modules/superagent/lib/node/index.js:591:12)
      at IncomingMessage.<anonymous> (node_modules/superagent/lib/node/index.js:767:18)
      at endReadableNT (_stream_readable.js:975:12)
      at _combinedTickCallback (internal/process/next_tick.js:74:11)
      at process._tickCallback (internal/process/next_tick.js:98:9)

So, I am expecting a "Hello, world", but instead I get a full fledged HTML page that looks like the following:

所以,我期待一个“Hello,world”,但我得到了一个完整的HTML页面,如下所示:

So the way I see it, I must first login with my testing script before testing anything.

所以我看到它的方式,我必须首先使用我的测试脚本登录才能测试任何东西。

Code

To test my app, I am using the following code:

要测试我的应用程序,我使用以下代码:

"use strict";

let app = require("./server.js");
var assert = require("assert");
let superagent = require("superagent");

describe("server", function() {
    let server;

    beforeEach(function() {
        server = app().listen("8080");
    });

    afterEach(function() {
        server.close();
    });

    it("prints out 'Hello, world' when user goes to /", function(done) {

        superagent.get("https://workspace-username.c9users.io/", function(error, res) {
            assert.ifError(error);
            assert.equal(res.status, 200);
            assert.equal(res.text, "Hello, world");
            done();
        });
    });
});

I have considered the fact that my URL is wrong, and that I should use both process.env.PORT instead of "8080" and process.env.IP instead of "https://workspace-username.c9users.io/", but after making this substitution, nothing worked either.

我考虑过我的网址错误,我应该使用process.env.PORT而不是“8080”和process.env.IP而不是“https://workspace-username.c9users.io/”,但在进行这种替换之后,也没有任何效果。

Questions:

  1. Am I trying to access localhost the correct way?
  2. 我试图以正确的方式访问localhost吗?

  3. Am I really forced to login if I want to debug ?
  4. 如果我想调试,我真的被迫登录吗?

  5. If so, how can I do it?
  6. 如果是这样,我该怎么办?

1 个解决方案

#1


1  

It would appear that your application url is private at the moment, so you're seeing a login page from Cloud9 when you try to access it.

现在看来您的应用程序URL是私有的,因此当您尝试访问它时,您会看到来自Cloud9的登录页面。

You can view my answer here: https://stackoverflow.com/a/29468429/722657 to get details on how to make your application url public for API use.

您可以在此处查看我的答案:https://stackoverflow.com/a/29468429/722657以获取有关如何使您的应用程序URL公开以供API使用的详细信息。

Hope this helps.

希望这可以帮助。


分享到: