问题描述
我想参加测试的班级
using System;
using System.Net;
using System.Net.sockets;
using System.Text;
namespace HttpServer
{
class Program
{
static void Main(string[] args)
{
var webserver = new Program();
webserver.Listen(12345);
}
public static void ProcesstcpClientAsync(TcpClient tcpClient)
{
// Get a stream object for reading and writing
NetworkStream networkStream = tcpClient.GetStream();
byte[] buffer = readByteBufferFromNetworkStream(networkStream);
string request = Encoding.UTF8.GetString(buffer);
Console.WriteLine(request);
const string HTTPNewLine = "\r\n";
string html = "<h1>Hello from TestServer</h1>";
string response = "HTTP/1.1 200 OK" + HTTPNewLine;
response += "Server: TestServer 2020" + HTTPNewLine;
response += "Content-Type: text/html; charset=utf-8" + HTTPNewLine;
response += "Content-Length: " + html.Length + HTTPNewLine;
response += HTTPNewLine;
response += html;
response += HTTPNewLine;
byte[] responseBytes = Encoding.UTF8.GetBytes(response);
networkStream.Write(responseBytes);
networkStream.Close();
tcpClient.Close();
}
public void Listen(Int32 port)
{
//Int32 port = 12345;
IPAddress localaddr = IPAddress.Parse("127.0.0.1");
TcpListener tcpListener = new TcpListener(localaddr,port);
tcpListener.Start();
// Enter the listening loop (service/daemon)
while (true)
{
//@ Sync Version for debugging
TcpClient tcpClient = tcpListener.AcceptTcpClient();
ProcesstcpClientAsync(tcpClient);
//tcpListener.AcceptTcpClientAsync().ContinueWith((res) => ProcesstcpClientAsync(res.GetAwaiter().GetResult()));
}
}
public static byte[] readByteBufferFromNetworkStream(NetworkStream networkStream)
{
byte[] buffer = new byte[256];
int bytesRead = 0;
while (true)
{
int currentBytesRead = networkStream.Read(buffer,bytesRead,buffer.Length - bytesRead);
bytesRead += currentBytesRead;
// If we've read less data than the size of our buffer -> it means there is no more information,so break.
if (currentBytesRead < buffer.Length) { break; }
// If we've reached the end of our buffer -> check to see if there's any more information,or break.
if (bytesRead == buffer.Length)
{
int nextByte = networkStream.ReadByte();
// End of stream? If so,we're done.
if (nextByte == -1)
{
break;
}
// Otherwise resize the buffer,put in the byte we've just read and continue.
else
{
byte[] newBuffer = new byte[buffer.Length * 2];
Array.copy(buffer,newBuffer,buffer.Length);
newBuffer[bytesRead] = (byte)nextByte;
buffer = newBuffer;
bytesRead++;
}
}
}
// Buffer is Now too big. Shrink it.
byte[] resultBuffer = new byte[bytesRead];
Array.copy(buffer,resultBuffer,bytesRead);
return resultBuffer;
}
}
}
存根类
@stubOnTest('CategorizationServiceStub')
class CategorizationService {
...
装饰器可以“捕获”对import CategorizationService from '../../server/services/CategorizationService';
export default class CategorizationServiceStub extends CategorizationService {
....
的调用,并将其替换为模拟
CategorizationService
但是当调用export default function stubOnTest(stubbedClassstr: string) {
const pathToStubbedClass = path.resolve(__dirname,stubsDir,stubbedClassstr);
const stubbedClass = require(pathToStubbedClass);
...
时,它会扩展CategorizationServiceStub
并调用装饰器CategorizationService
。
然后它变成了一个无限循环。
有办法避免这种情况吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)