MailNews fakeserver 编辑

Fakeserver is a testing server that implements a generic text-based protocol common to all major mailnews protocols (POP, IMAP, SMTP, and NNTP) designed for use in automated tests. It can also be used in manual QA tests.

Basic Structure

Fakeserver is situated entirely under mailnews/test/fakeserver/. It is comprised of three major components: a daemon, a handler, and the server itself.

The server itself is found in mailnews/test/fakeserver/maild.js and was largely based off of the httpd fakeserver from network code. It utilizes UTF-8 as its transport mechanism and is capable of performing proper pipelining of commands.

The other two components are specific to the protocol type and found under similar files in the same directory (e.g., smtpd.js).

A daemon is information about the server that can be manipulated. An example of such a manipulation is adding a folder or adding messages. Daemons should usually persist for the entirety of the test.

The handler is the glue between the server and the daemon. This area will be the most diverse of all components, as edge cases with quirky servers being liberal with the specs will require different handlers.

Writing a new fakeserver

Since the number of protocols we use is very small, most people shouldn't have to write an entirely new daemon and handler setup by themselves. That said, someone writing a new test may have to take into account a new handler.

A handler will contain the following methods:

<caption> Handler methods </caption>
NameArgumentsReturnsNotes
[command]rest of sent lineServer's responseThe name is normalized to be uppercase.
onErrorcommand, rest of sent lineServer's responseCalled if handler does not define the command function.
onStartupnoneServer's responseCalled when a connection is made.
onMultilinesent lineServer's response or nothingCalled when in multi-line mode.
postCommandServer objectnoneCalled after every command.

The server is smart about the responses and will normalize all occurrences of '\n' (but not '\r') to '\r\n' for you, as well as append the '\r\n' to the response if not present. It will also properly queue all messages sent in pipelining mode and will present lines without any EOL characters. In addition, it uses UTF-8 as the transmission medium.

The server presents the following API to the handler:

<caption> Server API </caption>
NameArgumentsReturnsDescription
closeSocketnonenothingCloses the socket and stops the test.
setMultilinethe new value of multilinenothingThe value is a boolean, with true invoking multiline mode. It is off by default.

Extending a handler to provide different mechanisms is a process not defined here; please check the documentation of the corresponding handler for instructions.

Using fakeserver in xpcshell tests

Since there are three different components of fakeserver, a total of four objects need to be set up in a fakeserver test on top of other testing. The outline of a test looks like this:

var daemon = setupDaemon(); // Check with your local fakeserver docs
var handlers = [ /* Some set of handlers */ ];

for (var handler in handlers) {
  // This sets up the fake server
  var server = new nsMailServer(new handler(daemon));
  // Port to use. I tend to like using 1024 + default port number myself.
  // Probably best to include as a constant somewhere. Also, use something
  // larger than 1024 for best results.
  server.start(port);

  // Set up a nsIMsgIncomingServer locally
  localserver.someActionRequiringConnection();
  server.performTest();
  // Nothing will be executed until the connection is closed
  // localserver.closeCachedConnections() is generally a good way to do so
  server.resetTest();

  // Set up second test
  server.performTest();
  transaction = server.playTransaction();

  // Finished with tests
  server.stop();
}

Currently, fakeserver provides no means to keep a persistent connection past a test, requiring connections to be closed, possibly forcibly. Alternatively, one can wait for the timeout, which occurs after 3 minutes, so not closing connections would make the tests take unbearably long.

The server provides the following api to xpcshell tests:

<caption> nsMailServer xpcshell API </caption>
NameArgumentsReturnsDescription
performTestnonenothingRuns until the test is forcibly aborted or stopped normally
isStoppednoneif the server is stoppedhelper for performTest
isTestFinishednoneif the test is finishedhelper for performTest
playTransactionnonethe transactionThe transaction is an object with two properties: us, and them; us is an array of responses we sent, them an array of commands received
resetTestnonenothingprepares the server for the next test without closing the connection
startport numbernothingStarts the server listening
stopnonenothingStops the server and closes the connection

Using fakeserver in QA testing

Debug output from fakeservers

It is possible to get the fakeservers to dump to the console the commands they have sent and received.

To enable/disable debugging, call setDebugLevel on your nsMailServer instance with one of the following options:

const fsDebugNone = 0;     // Dump nothing
const fsDebugRecv = 1;     // Dump just the received commands
const fsDebugRecvSend = 2; // Dump received + sent commands, no data
const fsDebugAll = 3;      // Dump everything

Debugging is set to "None" by default.

Specific fakeserver guidelines

POP

 

IMAP

 

SMTP

 

NNTP

nntpd.js defines a few different classes of NNTP servers: compliance for RFC 977, RFC 2980, and RFC 3977, as well as a Giganews and an INN server. (Full 2980 and 3977 compliance as well as INN is not yet written).

For xpcshell tests in mailnews/news, all files in mailnews/news/test/postings/auto-add are automatically added based on the Newsgroups header. There are added in alphabetical order, which may help for purposes that rely on article key orders. The only generated header is Lines.

The following comprises the API of the news fakeserver itself:

nntpDaemon API
NameArgumentsReturnsNotes
[constructor]daemon flagsN/AFlags are defined below
addGroupgroup name (string), is postablenothingAdds the group (resetting if it exists)
addArticlenewsArticle objectnothingAdds the message to all groups in the article's group list
addArticleToGroupnewsArticle object, group (string), integral key for groupnothingAdds the message to the specified group with the specified key
getGroupgroup namegroup objectGroup is a map key->article, with the additional properties: flags, keys (array of keys), nextKey (next key to use for new articles)
getGroupStatsgroup namearray of [size, min, max]the size is an estimate if NNTP_REAL_LENGTH is not specified in the daemon flags.
getArticlemessage IDnewsArticle objectPretty self-explanatory
newsArticle API
NameArgumentsReturnsNotes
[constructor]text (as a string)N/Ainitializes all fields
headers(property)map of header (lower-case) -> value
body(property)text of body
messageID(property)message ID
fullText(property)Full text as message without modification except added headers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:23 次

字数:11068

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文