protected override bool PortResponse(PortEvent pEvent) { if (pEvent.Name == Test.Steps[CurrentStep].Actor.Name) { try { if ((!string.IsNullOrEmpty(CurrentResponse)) && (CurrentResponse.Contains(pEvent.Message))) { return(false); } // TEST ONLY... //System.Threading.Thread.Sleep(100); // DESIGN NOTE: // - Finding the appropriate response by command allows the responses to be out of sequence with the steps of a test // PROBLEM: Each response must be unique to allow lookup. This means if a test contains duplicate commands the count of responses will be less than the count of steps in the test. CurrentResponse = FindResponse(pEvent.Message == Test.Steps[CurrentStep].Actor.Action ? Test.Steps[CurrentStep].Actor.Action : pEvent.Message.IndexOf(' ') == -1 ? pEvent.Message : pEvent.Message.Substring(0, pEvent.Message.IndexOf(' ')).Trim()); Step(new TestEvent(InternalSignal.TestStep, Name), Retries); if (++Retries > Test.Steps[CurrentStep].Retries) { NextStep(); } } catch (Exception ex) { TransitionToFailed(ex.Message); } } return(false); }
protected override void Step(TestEvent tEvent, int retries) { if (tEvent.Name == Name) { Retries = retries; switch (Mode) { case UUTTestMode.Port: qf4net.QF.Instance.Publish(new PortEvent(QFSignal.PortTransmit, Test.Steps[CurrentStep].Actor.Name, CurrentResponse)); break; case UUTTestMode.Direct: string delimiter = "\r\n"; if (Test.Steps[CurrentStep].Response != null) { delimiter = Test.Steps[CurrentStep].Response.Delimiter; } foreach (string element in CurrentResponse.Split(new string[] { delimiter }, StringSplitOptions.RemoveEmptyEntries)) { qf4net.QF.Instance.Publish(new PortEvent(QFSignal.PortResponse, Test.Steps[CurrentStep].Actor.Name, element)); } break; } } }
/// <summary> /// 【客户端】socket协议得到的数据转成当前请求响应,步骤【四】 /// </summary> /// <param name="e"></param> /// <returns></returns> public CurrentResponse ToResponse(OnReceivedSocketEventArgs e) { int offset = 0; var resp = new CurrentResponse() { Id = this.DecodeUInt64(e.Buffer, offset, out offset), }; //读取encoding var encoding = this.DecodeEncoding(e.Buffer, Encoding.ASCII, offset, out offset); //读取命令 var command = this.DecodeString(e.Buffer, encoding, offset, out offset); //设置命令 var response = new Response(encoding, command); resp.Response = response; response.Query = this.DecodeNameValueCollection(e.Buffer, encoding, offset, out offset); response.Form = this.DecodeNameValueCollection(e.Buffer, encoding, offset, out offset); response.Headers = this.DecodeNameValueCollection(e.Buffer, encoding, offset, out offset); //设置body var bodylength = this.DecodeInt64(e.Buffer, offset, out offset); if (bodylength > 0) { var memorystream = new MemoryStream(e.Buffer, offset, e.Buffer.Length - offset); memorystream.Flush(); response.Body = memorystream; } return(resp); }
public void TestRemoteProtocol() { var protocol = new Protocol(); var request = new CurrentRequest() { Request = new Request(Encoding.UTF8, "test"), Id = 636 }; ((Request)request.Request).Writer.Write("abc"); var bytes = protocol.FromRequest(request); var request2 = protocol.ToRequest(new Never.Sockets.AsyncArgs.OnReceivedSocketEventArgs(null, bytes) { }); var abc = Encoding.UTF8.GetString((((Request)request2.Request).Body as MemoryStream).ToArray()); var response1 = protocol.ToResponse(new Never.Sockets.AsyncArgs.OnReceivedSocketEventArgs(null, bytes) { }); abc = Encoding.UTF8.GetString((((Response)response1.Response).Body as MemoryStream).ToArray()); var response = new CurrentResponse() { Response = new Response(Encoding.UTF8, "test"), Id = 636, }; var handlerresponse = new ResponseResult() { Query = new System.Collections.Specialized.NameValueCollection(), }; handlerresponse.Query.Add("abc", "efg"); handlerresponse.Body = new MemoryStream(Encoding.UTF8.GetBytes("abc")); bytes = protocol.FromResponse(request, handlerresponse); var response2 = protocol.ToResponse(new Never.Sockets.AsyncArgs.OnReceivedSocketEventArgs(null, bytes) { }); abc = Encoding.UTF8.GetString((((Response)response2.Response).Body as MemoryStream).ToArray()); }
public async Task <IActionResult> CurrentByLonLat(string lat, string lon) { if (lat == null || lon == null) { return(BadRequest("Invalid Data Entered")); } try { var apicall = ApiCall.CallAsync <CurrentResponse>($"http://api.weatherapi.com/v1/current.json?key={Secret.weatherapikey}&q={HttpUtility.UrlEncode(lat)},{HttpUtility.UrlEncode(lon)}"); CurrentResponse apiresponse = (CurrentResponse)apicall.Result; return(Ok(apiresponse)); } catch (Exception e) { return(NotFound()); } }
public async Task <IActionResult> DailyByName(string cityName) { if (cityName == null) { return(BadRequest("Invalid Data Entered")); } try { var apicall = ApiCall.CallAsync <CurrentResponse>($"http://api.weatherapi.com/v1/current.json?key={Secret.weatherapikey}&q={HttpUtility.UrlEncode(cityName)}"); CurrentResponse apiresponse = (CurrentResponse)apicall.Result; return(Ok(apiresponse)); } catch (Exception e) { return(NotFound(e)); } }
private async Task WriteData(string payload, bool intermediateMessages = false, bool saleDetail = false, bool printOnPOS = true) { Port.DiscardInBuffer(); Port.DiscardOutBuffer(); CurrentResponse = ""; if (Instance.CantWrite()) { throw new TransbankException($"Unable to send message to {Port.PortName}"); } Port.Write(payload); bool ack = ReadAck(new CancellationTokenSource(_defaultTimeout).Token); if (ack) { Console.WriteLine("Read ACK OK"); if (intermediateMessages) { await ReadMessage(new CancellationTokenSource(_defaultTimeout).Token); string responseCode = CurrentResponse.Substring(1).Split('|')[1]; while (responseCode == "78" || responseCode == "79" || responseCode == "80" || responseCode == "81" || responseCode == "82") { await ReadMessage(new CancellationTokenSource(_defaultTimeout).Token); responseCode = CurrentResponse.Substring(1).Split('|')[1]; } } else { if (saleDetail) { SaleDetail = new List <string>(); if (!printOnPOS) { await ReadMessage(new CancellationTokenSource(_defaultTimeout).Token); string authorizationCode; try { authorizationCode = CurrentResponse.Substring(1).Split('|')[5]; if (authorizationCode != "" && authorizationCode != " ") { SaleDetail.Add(string.Copy(CurrentResponse)); } } catch (IndexOutOfRangeException) { authorizationCode = null; } while (authorizationCode != null && authorizationCode != "" && authorizationCode != " ") { await ReadMessage(new CancellationTokenSource(_defaultTimeout).Token); try { authorizationCode = CurrentResponse.Substring(1).Split('|')[5]; if (authorizationCode != "" && authorizationCode != " ") { SaleDetail.Add(string.Copy(CurrentResponse)); } } catch (IndexOutOfRangeException) { authorizationCode = null; } } } } else { await ReadMessage(new CancellationTokenSource(_defaultTimeout).Token); } } } else { throw new TransbankException($"Unable to send message to {Port.PortName}"); } }