Data simulation method and device based on front-end code file, equipment and medium
By adding simulated annotation data to the front-end code file to generate a simulated configuration file and automatically create simulated interfaces, the problem of low development efficiency caused by setting up a simulated server is solved, and efficient interface simulation and rapid iteration are achieved.
Patent Information
- Application Number
- CN202510060263.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-15
- Publication Date
- 2025-11-07
- Estimated Expiration
- 2045-01-15
AI Technical Summary
In the process of front-end development and testing, setting up a simulation server requires configuration and debugging, which consumes development time and affects efficiency. Furthermore, due to the high complexity of the interaction, configuring the interface is time-consuming and labor-intensive, which reduces development efficiency.
By adding simulated comment data to the code comments in the front-end code file, a simulated configuration file is generated, a simulated interface is automatically created, HTTP requests are intercepted and simulated data is generated based on the requests, avoiding the need to set up a simulated server and directly implementing the simulated interface function on the front end.
It reduces the tedious steps of setting up a simulation server, improves development efficiency, enables the rapid creation of various simulation scenarios to meet different testing needs, reduces development workload, and adapts to dynamic changes during interface iteration.
Smart Images

Figure CN119961072B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of Internet, in particular to the technical field of interface data simulation, and particularly relates to a data simulation method and device based on front-end code files, equipment and medium. BACKGROUND
[0002] At present, in the front-end development test process, interface simulation is an indispensable step. Interface simulation mainly solves the problem of parallel development of front-end and back-end separation and the problem of interface development of front-end needing back-end interface data. Specifically, a simulation server is built, and a configuration file is manually created by a developer to store simulation data on the simulation server, and the interface is forwarded in cooperation with the local development environment. In this way, data simulation is completed for the interface that has not been developed.
[0003] However, the simulation server needs to be configured and debugged, which occupies the development time and affects the development efficiency. In addition, due to the increasing complexity of the current application interaction, a page usually needs to call multiple interfaces, and it takes time and effort to configure these interfaces, so the manual creation of the configuration file by the developer also reduces the development efficiency. SUMMARY
[0004] Therefore, the embodiments of the present application provide a data simulation method and device based on front-end code files, equipment, medium and products, which can omit the building step of the simulation server and automatically create a simulation configuration file based on the simulation annotation data input by the user, thereby improving the development efficiency.
[0005] In a first aspect, the embodiments of the present application provide a data simulation method based on a front-end code file, the method comprising: in the case of obtaining a front-end code file of an http page, adding simulation comment data corresponding to a page simulation event in a code comment of the front-end code file based on user input, wherein the simulation comment data comprises a request path, a request parameter, a request response status code, a request response body and a simulation switch field of a simulation interface, and the simulation switch field is used to represent that the simulation interface is in a simulation open state or a simulation closed state; generating a request identifier in combination with the request path and the request parameter in the simulation comment data, and generating simulation data in combination with the request response status code, the request response body and the simulation switch field; generating simulation configuration data corresponding to the page simulation event based on the request identifier and the simulation data, and writing the simulation configuration data into a simulation configuration file, wherein the simulation configuration file contains simulation configuration data corresponding to each page simulation event of the http page; in the case of listening to an http request of the http page, intercepting the http request, and generating a target request identifier based on a request path and a request parameter in the http request; querying target simulation configuration data matched with the target request identifier from the simulation configuration file based on the target request identifier, returning simulation data in the target simulation configuration data as a response result only in the case that the simulation switch field in the target simulation configuration data is used to represent the simulation open state, otherwise obtaining real response data as the response result by sending an http request to a back-end server.
[0006] In a second aspect, an embodiment of the present application provides a data simulation device based on a front-end code file, the device comprising: an adding module configured to add simulation comment data corresponding to a page simulation event in a code comment of the front-end code file based on user input, in a case where the front-end code file of an HTTP page is acquired, wherein the simulation comment data comprises a request path, request parameters, a request response status code, a request response body, and a simulation switch field of a simulation interface, and the simulation switch field is used to represent that the simulation interface is in a simulation on state or a simulation off state; a generating module configured to generate a request identifier in combination with the request path and the request parameters in the simulation comment data, and generate simulation data in combination with the request response status code, the request response body, and the simulation switch field; a writing module configured to generate simulation configuration data corresponding to the page simulation event based on the request identifier and the simulation data, and write the simulation configuration data into a simulation configuration file, wherein the simulation configuration file comprises simulation configuration data corresponding to each page simulation event of the HTTP page; an interception generating module configured to intercept an HTTP request in a case where the HTTP request of the HTTP page is listened to, and generate a target request identifier based on a request path and request parameters in the HTTP request; and a querying module configured to query target simulation configuration data matched with the target request identifier from the simulation configuration file based on the target request identifier, return simulation data in the target simulation configuration data as a response result only in a case where the simulation switch field in the target simulation configuration data is used to represent the simulation on state, or otherwise acquire real response data as the response result by sending an HTTP request to a back-end server.
[0007] In a third aspect, an embodiment of the present application provides an electronic device, comprising a processor and a memory storing computer program instructions; and the processor implements the steps of the data simulation method based on the front-end code file according to the first aspect when executing the computer program instructions.
[0008] In a fourth aspect, an embodiment of the present application provides a computer readable storage medium, and the computer readable storage medium stores computer program instructions, and the computer program instructions are executed by a processor to implement the steps of the data simulation method based on the front-end code file according to the first aspect.
[0009] In a fifth aspect, an embodiment of the present application provides a computer program product stored in a non-volatile storage medium, and the computer program product is executed by a processor to implement the steps of the data simulation method based on the front-end code file according to the first aspect.
[0010] In a sixth aspect, an embodiment of the present application provides a chip, comprising a processor and a communication interface, the communication interface and the processor are coupled, and the processor is configured to run a program or instructions to implement the steps of the data simulation method based on the front-end code file according to the first aspect.
[0011] The application provides a data simulation method and device based on a front-end code file, equipment, medium and product. A developer can add simulation comment data corresponding to a page simulation event in a code comment of a front-end code file based on user input, generate a request identifier in combination with a request path and request parameters in the simulation comment data, and generate simulation data in combination with a request response status code, a request response body and a simulation switch field. Based on the request identifier and the simulation data, simulation configuration data corresponding to the page simulation event is generated and written into a simulation configuration file. Based on this, when an HTTP request of an HTTP page is listened to, the HTTP request is intercepted, a target request identifier is generated based on a request path and request parameters in the HTTP request, and target simulation configuration data matched with the target request identifier is queried from the simulation configuration file as a response result based on the target request identifier. In this way, compared with a traditional way, in order to simulate a back-end interface, a developer needs to build a simulation server and perform server configuration, maintenance, performance optimization and other problems. The application avoids these cumbersome steps and only needs to add simulation comment data in the front-end code file to realize the function of simulating an interface, replacing an original back-end response, so that a developer can complete debugging and testing without relying on a real back-end. Moreover, by modifying simulation comment data in a code comment, simulation configuration data can be updated, a developer can easily adjust the response behavior of a simulation interface, different testing requirements can be met, and various simulation scenarios can be quickly created. The simulation comment data includes a request path, request parameters, a request response status code, a request response body and a simulation switch field of a simulation interface. The simulation switch field is used to represent that the simulation interface is in a simulation open state or a simulation closed state. Therefore, based on the simulation switch field, when the simulation interface data is needed, the simulation switch field is set to the simulation open state, simulation data in the target simulation configuration data is returned as a response result, it is ensured that when the simulation function is enabled, the system can correctly return preset simulation data, and when the simulation interface data is not needed, the simulation switch field is set to the simulation closed state, and real response data is obtained by sending an HTTP request to a back-end server as a response result. This process is reiteration for an existing interface, can reduce simulation data configuration work, can meet the simulation requirements of dynamic changes of an interface in an iteration process, only needs to modify a field value of the simulation switch field, does not need to frequently delete and add simulation data in the front-end code file, reduces development workload, and further improves development efficiency. BRIEF DESCRIPTION OF DRAWINGS
[0012] In order to more clearly illustrate the technical solutions of the embodiments of the application, the following briefly introduces the drawings in the embodiments of the application.
[0013] Figure 1 is a flowchart of a data simulation method based on a front-end code file provided by an embodiment of the application;
[0014] Figure 2 is an exemplary schematic diagram of data merging provided by an embodiment of the present application;
[0015] Figure 3 is a structural schematic diagram of a data simulation device based on a front-end code file provided by an embodiment of the present application;
[0016] Figure 4 is a hardware structural schematic diagram of an electronic device provided by an embodiment of the present application. DETAILED DESCRIPTION
[0017] The principles and spirits of the present application will be described below with reference to a number of exemplary embodiments. It should be understood that the purpose of providing these embodiments is to make the principles and spirits of the present application clearer and more thorough, so that those skilled in the art can better understand and implement the principles and spirits of the present application. The exemplary embodiments provided herein are only a part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments herein, all other embodiments obtained by those skilled in the art without creative labor are within the scope of protection of the present application.
[0018] In this document, terms such as first, second, third, etc. are used only to distinguish one entity (or operation) from another entity (or operation), and do not imply or suggest any order or association between the entities (or operations).
[0019] The data simulation method based on a front-end code file provided by an embodiment of the present application will be described in detail below with reference to specific embodiments and their application scenarios.
[0020] Figure 1 is a flow schematic diagram of a data simulation method based on a front-end code file provided by an embodiment of the present application. The execution subject of the data simulation method based on a front-end code file can be a system, which can include a user terminal, a development server, and an Application Programming Interface (API) server.
[0021] The development server allows developers to deploy the code being written to the server and perform real-time testing and debugging. Through the development server, developers can simulate the use scenarios of real users and monitor and analyze the performance of the software.
[0022] The development server is the center of receiving, processing, and storing user terminal data. The user terminal communicates with the development server by sending requests (such as HTTP requests), and the development server processes the corresponding business logic according to the request and returns the result data to the user terminal.
[0023] The user terminal is installed with a browser, and the user inputs the website address or clicks the link through the browser interface on the user terminal (such as a computer or a mobile phone) to access the resources on the Internet. After receiving the user's instruction, the browser communicates with the API server, requests and receives the web page content. Then, the browser renders these contents on the user's screen, enabling the user to browse and read the web page information. The browser is responsible for rendering the user interface, responding to the user's input and operation, and feeding back the results to the user.
[0024] The following will take the system as an example of the execution subject of the data simulation method based on the front-end code file to illustrate the data simulation method based on the front-end code file of the present application. It should be noted that the above execution subject and application scenarios do not constitute a limitation on the present application.
[0025] As shown in Figure 1 The data simulation method based on the front-end code file provided by the embodiments of the present application can include steps 110-150.
[0026] Step 110, in the case of obtaining the front-end code file of the http page, adding the simulation annotation data corresponding to the page simulation event in the code annotation of the front-end code file based on the user input;
[0027] Step 120, generating a request identifier in combination with the request path and the request parameter in the simulation annotation data, and generating simulation data in combination with the request response status code, the request response body and the simulation switch field;
[0028] Step 130, generating simulation configuration data corresponding to the page simulation event based on the request identifier and the simulation data, and writing the simulation configuration data into the simulation configuration file;
[0029] Step 140, in the case of listening to the http request of the http page, intercepting the http request, and generating a target request identifier based on the request path and the request parameter in the http request;
[0030] Step 150, based on the target request identifier, querying the target simulation configuration data matched with the target request identifier from the simulation configuration file, only in the case that the simulation switch field in the target simulation configuration data is used to represent the simulation opening state, the simulation data in the target simulation configuration data is returned as the response result, otherwise the real response data is obtained as the response result by sending the http request to the back-end server.
[0031] The method for simulating data based on a front-end code file provided in the embodiments of this application can be used to add simulation annotation data corresponding to a page simulation event in a code annotation of a front-end code file based on user input, generate a request identifier in combination with a request path and request parameters in the simulation annotation data, and generate simulation data in combination with a request response status code, a request response body and a simulation switch field. Based on the request identifier and the simulation data, simulation configuration data corresponding to the page simulation event is generated, and the simulation configuration data is written into a simulation configuration file. Based on this, when an http request of an http page is listened to, the http request is intercepted, a target request identifier is generated based on a request path and request parameters in the http request, and thus the target simulation configuration data matched with the target request identifier is queried from the simulation configuration file as a response result based on the target request identifier. In this way, compared with the traditional way, in order to simulate a back-end interface, a developer needs to build a simulation server and perform server configuration, maintenance, performance optimization and other problems, and the present application avoids these cumbersome steps and only needs to add simulation annotation data in a front-end code file to realize the function of simulating an interface. Moreover, the simulation configuration data can be updated by modifying the simulation annotation data in the code annotation, and the developer can easily adjust the response behavior of the simulation interface, which can meet different testing requirements and quickly create various simulation scenarios. The simulation annotation data includes a request path, request parameters, a request response status code, a request response body and a simulation switch field of the simulation interface. The simulation switch field is used to represent that the simulation interface is in a simulation open state or a simulation closed state. Therefore, based on the simulation switch field, the simulation interface data is set to the simulation open state when the simulation interface data is needed, and the simulation data in the target simulation configuration data is returned as a response result, and the simulation interface data is set to the simulation closed state when the simulation interface data is not needed, and the real response data is obtained by sending an http request to a back-end server as a response result. This process is a reiteration of an existing interface, which can reduce simulation data configuration work, meet the simulation requirements of dynamic changes of the interface in the iteration process, only needs to modify the field value of the simulation switch field, does not need to frequently delete and add simulation data in the front-end code file, reduces the development workload, and thus improves the development efficiency.
[0032] The specific implementation of the above steps will be described in detail in combination with specific embodiments.
[0033] In step 110, the simulation annotation data corresponding to the page simulation event is added in the code annotation of the front-end code file based on user input when the front-end code file of the http page is acquired.
[0034] In step 110, a front-end code file is configured for each http page, and the http page can be rendered by executing or running the front-end code file. The developer can manually add simulation comment data corresponding to a page simulation event in the code comments of the front-end code file on the development server. The page simulation event refers to an event created during front-end development to test the page behavior of the http page. These events can simulate user interaction behaviors such as clicking a button, submitting a form, loading a page, etc. By simulating these events, the developer can verify the logic and performance of the front-end code without relying on the back-end service.
[0035] The simulation interface refers to an interface created to simulate the back-end service. These interfaces can return preset data to simulate the response of the back-end service. Through the simulation interface, the developer can call these interfaces in the front-end code and process the returned data to verify the data interaction logic between the front-end and the back-end. The front-end developer can edit the simulation comment data of the simulation interface according to the needs, modify the content of specific fields, and better meet the testing needs or business logic for the simulation interface.
[0036] The page simulation event can trigger the call of the simulation interface. For example, when a user clicks a button, an event can be triggered, which in turn calls a simulation interface to obtain data.
[0037] The simulation comment data can include the request path, request parameters, request response status code, request response body, and simulation switch field of the simulation interface. The simulation switch field is used to represent the simulation interface in the simulation open state or the simulation closed state.
[0038] Specifically, the request path, request parameters, request response status code, request response body, and simulation switch field of the simulation interface are @url, @data (multiple parameters separated by commas), @statusCode, @body, and @usemock (representing whether to enable simulation data). If the "usemock" field value is true, it indicates that the current simulation interface is in the simulation open state, and the simulation data matching is enabled subsequently. If the "usemock" field value is false, it indicates that the current simulation interface is in the simulation closed state, and the simulation data matching is closed subsequently. The status code is an important part of the HTTP response, which uses three digits to represent the processing result of the server to the request. In the simulation configuration, the statusCode field is used to specify the HTTP status code of the simulation response.
[0039] Common status codes can include:
[0040] · 200: indicates that the request is successful
[0041] • 400: indicates that the client request is incorrect
[0042] • 404: indicates that the requested resource does not exist
[0043] • 500: indicates that the server has an internal error
[0044] In this way, by setting appropriate statusCode, different server response situations can be simulated, which helps front-end developers test the application behavior under various scenarios.
[0045] Before development, the interface field information is negotiated by front-end and back-end developers, and after the negotiation is completed, the front-end can add the interface information to the code comments.
[0046] For example, the page simulation event returns the jobId, 42373101 job description information through the case1 interface, the field value of @url is https: / / api.example.com / case1 / job / detail, and the field value of @data is jobId, 42373101. The following is the simulation annotation data added in the front-end code file for this page simulation event, where mockapi is the characteristic identifier of the simulation annotation data:
[0047] / **
[0048] *mockapi
[0049] *@url https: / / api.example.com / case1 / job / detail
[0050] *@datajobId,42373101
[0051] *@usemock true
[0052] *@statusCode 200
[0053] @body {"result":"1","code":"200","data":{"jobName":"Web front-end development","jobKeywords":"css html javascript debugging front-end html5 programming react front-end framework vue.js","jobStatus":"24","jobType":0,"jobWelfCode":"100830,100826","jobInfo":"
Job Content
[0054]
[0055] Involving step 120, the request identification is generated in combination with the request path and the request parameter in the simulation annotation data, and the simulation data is generated in combination with the request response status code, the request response body and the simulation switch field.
[0056] In step 120, the request path and the request parameter can be encrypted by using an encryption algorithm to obtain the request identification, and the request response status code, the request response body and the simulation switch field can be merged or combined to obtain the simulation data associated with the request identification.
[0057] In some embodiments of the present application, the step 120 generates the request identification in combination with the request path and the request parameter in the simulation annotation data, which can specifically include:
[0058] The field value of the request parameter is subjected to string segmentation processing to obtain a plurality of strings;
[0059] The field value of the request path and the plurality of strings are subjected to encryption processing by using a digest algorithm to obtain the request identification.
[0060] Exemplarily, the "data" field value is subjected to string separation processing to split into two strings "jobId" and "42373101", and then the three field values "https: / / api.example.com / case1 / job / detail", "jobId", and "42373101" are subjected to a digest algorithm to generate a request identifier, for example, a hash function (Message-Digest Algorithm 5, MD5), and the generated request identifier in the example is "9bddd1a0cc1c50d9".
[0061] In this way, the request path and the request parameter are subjected to encryption processing by using the digest algorithm, and the encryption result can be used as the request identifier to effectively identify the simulation configuration data in the simulation configuration file. When the http request is received subsequently, the simulation configuration data matched with the http request is filtered from the simulation configuration file based on the target request identifier generated from the http request, and the simulation configuration data matching scheme based on the request identifier is implemented. In addition, when the simulation configuration data is filtered from the simulation configuration file based on the target request identifier, it is ensured that only one piece of simulation configuration data matched with the target request identifier is returned, thereby ensuring the uniqueness of the response result.
[0062] It should be noted that, in addition to the digest algorithm, other encryption algorithms can be used to encrypt the field value and the plurality of strings of the request path to obtain the request identifier, which is not limited in the present application.
[0063] In some embodiments of the present application, after step 110 and before step 120, the method can further include: starting the front-end code file by using a front-end development tool (for example, webpack-dev-server); reading the front-end code file by using a parser (for example, a javascript parser acorn) to extract code annotations; in the code annotations, matching simulation annotation data according to a feature character, which is mockapi; parsing the simulation annotation data by using a syntax analysis tool to obtain a simulation annotation JSON object; and extracting the request path, the request parameter, the request response status code, the request response body, and the simulation switch field from the simulation annotation JSON object.
[0064] Step 130 involves generating simulation configuration data corresponding to the page simulation event based on the request identifier and the simulation data, and writing the simulation configuration data into the simulation configuration file.
[0065] In step 130, the combined request identifier and simulation data can obtain a complete simulation configuration data for subsequent http request response process, which can exist in the form of key-value pairs, the request identifier as the key and the simulation data as the value. The simulation configuration file can include simulation configuration data corresponding to each page simulation event of the http page.
[0066] As a specific example, based on the simulation annotation data in the above example, the following simulation configuration data can be generated:
[0067]
[0068]
[0069] It should be noted that at the same time, for a request identifier, there is only one simulation configuration data associated with it in the simulation configuration file. In subsequent interface iterations, the simulation configuration data corresponding to the request identifier can be updated by modifying the simulation data associated with the request identifier.
[0070] For example, for the request identifier "9bddd1a0cc1c50d9", the "usemock" field value is initially set to true, which can be adjusted to false subsequently.
[0071] In some embodiments of the present application, after generating the simulation configuration data corresponding to the page simulation event, the following steps can also be included:
[0072] Embedding the simulation configuration data into a preset code template to obtain an execution code;
[0073] Adding the execution code in the front-end code file to access the simulation configuration data in the global scope based on the execution code.
[0074] Specifically, the preset code template is used to indicate how to format the simulation configuration data. The present application can add the simulation annotation data corresponding to each page simulation event in the code annotation of the front-end code file based on user input, thereby obtaining multiple simulation configuration data. The data list of the multiple simulation configuration data is traversed, and each simulation configuration data is embedded into the preset code template. The program will generate complete code according to each configuration data, so as to compile the multiple simulation configuration data into JavaScript code. By adding the execution code in the front-end code file, the front-end code file is regenerated, and at this time, the front-end code file includes the request interception method and the simulation configuration data corresponding to each page simulation event.
[0075] Thus, after running the generated front-end code file in the browser, the simulation configuration data can be accessed in the global scope at the JavaScript runtime, and the simulation configuration file can be accessed, the matched simulation configuration data is obtained from the simulation configuration file based on the target request identifier, and it is ensured that the preconfigured simulation configuration data can be correctly returned.
[0076] Involving step 140, in the case of listening to the http request of the http page, the http request is intercepted, and the target request identifier is generated based on the request path and the request parameter in the http request.
[0077] In step 140, by running the request interception method in the front-end code file, the http request is listened to and intercepted, and the target request identifier is generated based on the request path and the request parameter in the http request in the same way as the above step 120, for example, by using the digest algorithm.
[0078] Specifically, XMLHttpRequest contains the open method, which is used to configure the browser request, and the following parameters can be obtained:
[0079] ·method - HTTP method. Usually "GET" or "POST".
[0080] ·URL - URL to request, usually a string.
[0081] The send method in XMLHttpRequest is used to send the browser request, and the following is obtained:
[0082] ·request data
[0083] Then the request path URL and the request parameter data are processed by using the digest algorithm, for example, as shown in the example, the request URL is "https: / / api.example.com / example / job / detail", and the data is "jobId":"42373101". The encoding "9bddd1a0cc1c50d9" is generated as the target request identifier by using the digest algorithm.
[0084] Involving step 150, based on the target request identifier, the target simulation configuration data matched with the target request identifier is queried from the simulation configuration file, only in the case that the simulation switch field in the target simulation configuration data is used to represent the simulation on state, the simulation data in the target simulation configuration data is returned as the response result, otherwise the real response data is obtained by sending the http request to the backend server as the response result.
[0085] In step 150, each piece of simulation configuration data contains a request identifier, and according to the generated target request identifier, the corresponding simulation configuration data is searched in the global scope, and the simulation configuration data carrying the target request identifier is determined as the target simulation configuration data. Moreover, it is queried whether the field value of the simulation switch field in the target simulation configuration data is true, and when it is true, it indicates that the simulation interface has started the simulation state, so the simulation data can be returned as the response result. When the corresponding backend interface of the simulation interface has been online, the field value of the simulation switch field can be set to false, and at this time, the http request is sent to the backend server to obtain the real response data as the response result.
[0086] Exemplarily, if the target request identifier is "9bddd1a0cc1c50d9", the simulation configuration data carrying "9bddd1a0cc1c50d9" can be determined as the target simulation configuration data, and the simulation data in the target simulation configuration data is determined as the simulation data matched with the target request identifier.
[0087] In some embodiments, in the case that the target simulation configuration data matched with the target request identifier is not queried from the simulation configuration file, the real response data is obtained as the response result by sending the http request to the backend server.
[0088] In this way, if the matched simulation data is found, the data is used as the response; if the matched simulation data is not found, the real network request is continued to be executed.
[0089] In some embodiments of the present application, the simulation annotation data corresponding to the page simulation event is added in the code annotation of the front-end code file in the above-mentioned step 110, which can specifically include:
[0090] In the case that the second interface is not online, the second interface is determined as the simulation interface, the request path, the request parameter, the request response status code and the request response body of the second interface are defined in the simulation annotation data, and the simulation switch field of the second interface is set to the simulation start state;
[0091] After the simulation annotation data corresponding to the page simulation event is added in the code annotation of the front-end code file, it further includes:
[0092] In the case that the second interface is in the online state, the simulation switch field of the second interface is changed to the simulation off state in the simulation annotation data, so that when the http request for calling the second interface is received, the real response data is obtained as the response result by sending the http request to the backend server;
[0093] In the case that the second interface is in the state to be updated, the request path, the request parameter, the request response status code and the request response body of the second interface are modified in the simulation annotation data, and the simulation switch field of the second interface is re-changed to the simulation open state.
[0094] The second interface can be any interface to be tested called by the http page.
[0095] For example, in the first iteration, interface A is not online, the backend is defined for the first time, and the front end needs to be simulated. At this time, interface A is used as a simulation interface, simulation annotation data of interface A is added in the front-end code file, and the simulation switch field of interface A is set to the simulation open state. In this way, the function of interface A is realized according to the simulation annotation data of interface A. In the second iteration, interface A has been online and has not been changed. At this time, simulation is not needed, so the simulation switch field of interface A is set to the simulation closed state. In the third iteration, the interface information of interface A is changed, and the backend modifies the interface definition. At this time, the front end needs to be simulated, so the simulation switch field of interface A is set to the simulation open state again.
[0096] In the embodiments of the present application, in the process of daily development iteration, there can be a case that the interface needs to be iterated multiple times after being online. If the simulation switch field is not set, after the second interface is online, the simulation annotation data of the second interface can only be deleted in the front-end code file. In the process of multiple iterations of the second interface in the future, the simulation annotation data needs to be added again in the front-end code to realize the simulation function of the second interface. In this way, it will cause the workload of the developer to increase. The simulation switch field of the present application can be used to control whether to enable the simulation data. By adding this field in the simulation annotation data, the simulation speed of the interface in the process of daily development iteration can be improved, and the interface simulation efficiency can be effectively improved, and the workload of the developer can be reduced.
[0097] In some embodiments of the present application, the simulation annotation data further includes a data merging switch field corresponding to the simulation interface. The data merging switch field is used to represent a merging open state or a merging closed state. The simulation data is generated based on the request response status code, the request response body, the simulation switch field and the data merging switch field. The step of returning the simulation data in the target simulation configuration data as the response result in the above step 150 can specifically include:
[0098] In the case that the target simulation configuration data is queried, the data merging switch field in the target simulation configuration data is parsed;
[0099] In the case that the data merging switch field is used to represent the merging closed state, only the simulation data in the target simulation configuration data is returned as the response result;
[0100] In a case where the data merging switch field is used to represent a merging on state, real response data is obtained by sending an http request to a backend server, and the real response data is merged with simulation data and returned as a response result.
[0101] Specifically, the data merging switch field is used to indicate whether simulation data is merged with real response data, where a request response body of the simulation interface can be a to-be-tested field, which can be selected by a user from all response fields of the simulation interface, for example, an updated response field of an online interface in an iteration process.
[0102] The data merging switch field can be usemerge, when a field value of usemerge is set to true, a data merging function is enabled, indicating that simulation data and real response data can be used for this response, where the real response data is obtained by sending an http request to a backend server. When the field value of usemerge is set to false, the data merging function is disabled.
[0103] In an embodiment of the present application, in an iteration process of an online interface, a developer can use the online interface as a simulation interface, write an updated response field of the online interface in an iteration process into simulation annotation data, and set the data merging switch field to a merging on state. Based on this, when an http request for calling the online interface is received, real response data and simulation data can be merged and returned after the interface response is successful. The original response field for the http request can be obtained from the real response data, and the updated response field can be obtained from the simulation data. This merging strategy ensures that the developer can flexibly control which field data comes from simulation data and which field data comes from a real interface, thereby realizing more precise interface simulation and testing, accurately testing the update function of the online interface, and ensuring that the developer can verify whether the function or interface design meets the expectation.
[0104] In some embodiments of the present application, the to-be-tested field can be a new response field, and the simulation annotation data corresponding to the page simulation event is added in the code annotation of the front-end code file in step 110, which can specifically include:
[0105] In a case where the first interface is in an iteration state, the first interface is determined to be a simulation interface, the request path, the request parameter, the request response status code, and the request response body of the first interface are defined in the simulation annotation data, and the data merging switch field of the first interface is set to a merging on state.
[0106] The first interface can be any to-be-tested interface called by an http page, and the first interface and the second interface can be the same interface or different interfaces, which are not limited in the present application.
[0107] For example, the original response fields of the first interface include "jobName", "jobKeywords", "jobStatus", "jobType", "jobWelfCode", "jobInfo", and "jobArea", and the new response fields of the first interface include "salary" and "degreeFrom" when the first interface is iterated. Based on this, "salary" and "degreeFrom" can be added to the front-end code file as the request response body of the first interface, so that when an http request for calling the first interface is received, as shown in the following formula, simulated data containing "salary" and "degreeFrom" and real response data containing the original response fields are obtained, and the two are combined to obtain combined data. Figure 2
[0108] In the embodiment of the present application, during the iteration of the online interface, the situation of adding new request response fields often occurs. At this time, in order to test the function of the updated interface, all the request response fields of the existing interface can be split, only the new response fields are tested, the new response fields are added to the front-end code file as simulated data, the original response fields do not need to be tested, and therefore the original response fields are still returned in the form of real response data. Subsequently, the two are directly combined based on the data merging switch field in the simulated data. In this way, all the request response fields of the online interface do not need to be configured in the front-end code file to return in the form of simulated data, unnecessary simulation configuration workload is reduced, the overall workload of simulation configuration during interface iteration is reduced, and the simulation test efficiency is improved.
[0109] In some embodiments of the present application, the above-mentioned real response data and simulated data after merging are returned as a response result, which can specifically include:
[0110] all fields in the simulated data are retained;
[0111] if a first field in the real response data does not exist in the simulated data, the first field is added in the response result;
[0112] if a second field exists in both the simulated data and the real response data, the second field in the real response data is deleted in the response result.
[0113] In the embodiment of the present application, the real response data and the simulation data follow strict merging rules. Since the request response body in the simulation data contains the to-be-tested field of the developer, by retaining all fields in the simulation data, effective simulation testing can be performed on all to-be-tested fields. If the first field in the real response data does not appear in the simulation data, it indicates that the first field is an interface response field that does not need to be tested, and also needs to be added to the response result for return, so as to avoid the absence of the response field in the response result of the http request and ensure the integrity of the response result. If the second field exists in both the simulation data and the real response data, it indicates that the second field is added to the simulation annotation data as a to-be-tested field, and only the second field in the simulation data needs to be retained for verification. Therefore, the second field in the real response data is deleted in the response result, so as to avoid the appearance of the duplicate field in the response result and ensure the correctness of the response result.
[0114] Corresponding to the method embodiment of the present application, the present application also provides a data simulation device based on a front-end code file.
[0115] Figure 3 is a structural schematic diagram of a data simulation device based on a front-end code file provided by the embodiment of the present application. As shown in Figure 3 the data simulation device based on a front-end code file 300 can include an adding module 310, a generating module 320, a writing module 330, an intercepting generating module 340, and a querying module 350.
[0116] The adding module 310 is configured to, in a case where the front-end code file of the HTTP page is acquired, add simulation annotation data corresponding to a page simulation event in a code annotation of the front-end code file based on a user input, wherein the simulation annotation data comprises a request path, request parameters, a request response status code, a request response body and a simulation switch field of a simulation interface, and the simulation switch field is used to represent that the simulation interface is in a simulation open state or a simulation close state; the generating module 320 is configured to generate a request identifier in combination with the request path and the request parameters in the simulation annotation data, and generate simulation data in combination with the request response status code, the request response body and the simulation switch field; the writing module 330 is configured to generate simulation configuration data corresponding to the page simulation event based on the request identifier and the simulation data, and write the simulation configuration data into a simulation configuration file, wherein the simulation configuration file comprises simulation configuration data corresponding to each page simulation event of the HTTP page; the intercepting and generating module 340 is configured to, in a case where an HTTP request of the HTTP page is listened to, intercept the HTTP request, and generate a target request identifier based on a request path and request parameters in the HTTP request; and the querying module 350 is configured to query target simulation configuration data matched with the target request identifier from the simulation configuration file based on the target request identifier, return simulation data in the target simulation configuration data as a response result only in a case where the simulation switch field in the target simulation configuration data is used to represent the simulation open state, or otherwise acquire real response data as the response result by sending an HTTP request to a back-end server.
[0117] The data simulation device based on the front-end code file provided in the embodiments of the present application can add simulation comment data corresponding to a page simulation event in a code comment of a front-end code file based on user input, generate a request identifier in combination with a request path and request parameters in the simulation comment data, and generate simulation data in combination with a request response status code, a request response body and a simulation switch field; based on the request identifier and the simulation data, simulation configuration data corresponding to the page simulation event is generated, and the simulation configuration data is written into a simulation configuration file. Based on this, when an http request of an http page is listened to, the http request is intercepted, a target request identifier is generated based on a request path and request parameters in the http request, and thus based on the target request identifier, target simulation configuration data matched with the target request identifier is queried from the simulation configuration file as a response result. In this way, compared with the traditional way, in order to simulate a back-end interface, a developer needs to build a simulation server and perform server configuration, maintenance, performance optimization and other problems, and the present application avoids these cumbersome steps and only needs to add simulation comment data in the front-end code file to realize the function of simulating an interface. Moreover, by modifying the simulation comment data in the code comment, the simulation configuration data can be updated, the developer can easily adjust the response behavior of the simulation interface, different test requirements can be met, and various simulation scenarios can be quickly created. The simulation comment data includes a request path, request parameters, a request response status code, a request response body and a simulation switch field of the simulation interface, the simulation switch field is used to represent that the simulation interface is in a simulation open state or a simulation closed state, and thus based on the simulation switch field, when the simulation interface data is needed, the simulation switch field is set to the simulation open state, and simulation data in the target simulation configuration data is returned as a response result, and when the simulation interface data is not needed, the simulation switch field is set to the simulation closed state, and real response data is obtained by sending an http request to a back-end server as a response result. This process is a reiteration of an existing interface, can reduce simulation data configuration work, can meet the simulation requirements of dynamic changes of the interface in an iteration process, only needs to modify a field value of the simulation switch field, does not need to frequently delete and add simulation data in the front-end code file, reduces the development workload, and further improves the development efficiency.
[0118] In some embodiments of the present application, further comprising: an embedding module, configured to embed the simulation configuration data into a preset code template to obtain an execution code after the simulation configuration data corresponding to the page simulation event is generated; and an accessing module, configured to add the execution code in the front-end code file to access the simulation configuration data in a global range based on the execution code.
[0119] In some embodiments of the present application, the generating module is specifically configured to: perform string segmentation processing on a field value of the request parameters to obtain a plurality of strings; and perform encryption processing on the field value of the request path and the plurality of strings by using a digest algorithm to obtain the request identifier.
[0120] In some embodiments of the application, the simulation annotation data further includes a data merging switch field corresponding to the simulation interface, the data merging switch field is used to represent a merging on state or a merging off state, the simulation data is generated based on the request response status code, the request response body, the simulation switch field and the data merging switch field, the query module includes: an analysis unit, configured to analyze the data merging switch field in the target simulation configuration data when the target simulation configuration data is queried; a return unit, configured to return only the simulation data in the target simulation configuration data as a response result when the data merging switch field is used to represent the merging off state; the return unit is further configured to acquire real response data by sending an http request to the backend server and return the real response data and the simulation data as a response result after merging when the data merging switch field is used to represent the merging on state.
[0121] In some embodiments of the application, the adding module is specifically configured to: in the case that the first interface is in an iterative state, determine that the first interface is a simulation interface, define the request path, the request parameter, the request response status code and the request response body of the first interface in the simulation annotation data, and set the data merging switch field of the first interface to a merging on state; wherein the request response body only contains an added response field of the first interface, and an original response field of the first interface is returned through real response data.
[0122] In some embodiments of the application, the return unit is specifically configured to: retain all fields in the simulation data; if a first field in the real response data does not exist in the simulation data, add the first field in the response result; if a second field exists in both the simulation data and the real response data, delete the second field in the real response data in the response result.
[0123] In some embodiments of the application, the adding module is specifically configured to: in the case that the second interface is not online, determine that the second interface is a simulation interface, define the request path, the request parameter, the request response status code and the request response body of the second interface in the simulation annotation data, and set the simulation switch field of the second interface to a simulation on state; after adding the simulation annotation data corresponding to the page simulation event in the code annotation of the front-end code file, further comprising: in the case that the second interface is in an online state, changing the simulation switch field of the second interface to a simulation off state in the simulation annotation data; in the case that the second interface is in an update pending state, modifying the request path, the request parameter, the request response status code and the request response body of the second interface in the simulation annotation data, and re-changing the simulation switch field of the second interface to a simulation on state.
[0124] The data simulation device based on a front-end code file provided by the embodiments of the application canFigure 1 The method embodiment of the application is the same as the process realized by the service platform, and the same technical effects can be realized. To avoid repetition, details are not described herein.
[0125] Figure 4 FIG. 1 is a schematic diagram of a hardware structure of an electronic device provided by an embodiment of the application.
[0126] As shown in FIG. 4, the electronic device 400 includes a memory 401, a processor 402, and a computer program stored in the memory 401 and executable on the processor 402. Figure 4
[0127] In one example, the processor 402 described above can include a central processing unit (CPU), or an application specific integrated circuit (ASIC), or can be configured as one or more integrated circuits that implement embodiments of the application.
[0128] The memory 401 can include a read-only memory (ROM), a random access memory (RAM), a magnetic disk storage medium device, an optical storage medium device, a flash memory device, an electrical, optical, or other physical / tangible memory storage device. Therefore, generally, the memory includes one or more tangible (non-transitory) computer-readable storage media (for example, memory devices) encoded with software including computer-executable instructions and, when the software is executed (for example, by one or more processors), is operable to perform the operations described with reference to the method of simulating data based on a front-end code file according to the embodiments of the first aspect of the application.
[0129] The processor 402 runs a computer program corresponding to the executable program code stored in the memory 401 by reading the executable program code, to implement the method of simulating data based on a front-end code file in the embodiments of the first aspect described above.
[0130] In some examples, the electronic device 400 can also include a communication interface 403 and a bus 410. As shown in FIG. 4, the memory 401, the processor 402, and the communication interface 403 are connected through the bus 410 and complete communication with each other. Figure 4
[0131] The communication interface 403 is mainly used to realize communication between modules, devices, units, and / or equipment in embodiments of the application. Input devices and / or output devices can also be accessed through the communication interface 403.
[0132] Bus 410 includes a hardware, software, or both, that couples components of electronic device 400 to each other. As an example and not by way of limitation, bus 410 can include an Accelerated Graphics Port (AGP) or other graphics bus, an Enhanced Industry Standard Architecture (EISA) bus, a Front Side Bus (FSB), a Hyper Transport (HT) interconnect, an Industry Standard Architecture (ISA) bus, an InfiniBand (IB) interconnect, a Low Pin Count (LPC) bus, a memory bus, a Micro Channel Architecture (MCA) bus, a Peripheral Component Interconnect (PCI) bus, a PCI-Express (PCI-E) bus, a Serial Advanced Technology Attachment (SATA) bus, a Video Electronics Standards Association Local (VLB) bus, or some other bus or combination of buses. Bus 410 can include one or more buses, where appropriate. Although this application describes and illustrates a particular bus, this application contemplates any suitable bus or interconnect.
[0133] The electronic device provided by the embodiments of the present application can realize Figure 1 The method embodiments of the present application can realize the various processes realized by the electronic device, and can realize the same technical effects. To avoid repetition, the same will not be described here.
[0134] In combination with the data simulation method based on the front-end code file in the above embodiments, the embodiments of the present application can provide a computer storage medium to realize. The computer storage medium has computer program instructions stored thereon; the computer program instructions are executed by a processor to realize the steps of any one of the data simulation methods based on the front-end code file in the above embodiments.
[0135] In combination with the data simulation method based on the front-end code file in the above embodiments, the embodiments of the present application can provide a computer program product to realize. The (computer) program product is stored in a non-volatile storage medium, and the program product is executed by at least one processor to realize the steps of any one of the data simulation methods based on the front-end code file in the above embodiments.
[0136] The chip according to the embodiments of the present application can also be referred to as a system chip, a system on chip, a chip system or a system on chip system.
[0137] It should be understood that the chip mentioned in the embodiments of the present application can also be referred to as a system chip, a system on chip, a chip system or a system on chip system.
[0138] It should be understood that the chip mentioned in the embodiments of the present application can also be referred to as a system chip, a system on chip, a chip system or a system on chip system.
[0139] The functional blocks shown in the structural block diagram described above can be implemented as hardware, software, firmware or a combination thereof. When implemented in hardware, it can be, for example, an electronic circuit, an application specific integrated circuit (ASIC), appropriate firmware, a plug-in, a functional card, etc. When implemented in software, the elements of the present application are program or code segments used to perform the required tasks. The program or code segments can be stored in a machine-readable medium or transmitted through a data signal carried in a carrier wave over a transmission medium or a communication link. The "machine-readable medium" can include any medium capable of storing or transmitting information. Examples of the machine-readable medium include an electronic circuit, a semiconductor memory device, a ROM, a flash memory, an erasable ROM (EROM), a floppy disk, a CD-ROM, an optical disk, a hard disk, an optical fiber medium, a radio frequency (RF) link, etc. The code segments can be downloaded via a computer network such as the Internet, an intranet, etc.
[0140] It should be understood that the exemplary embodiments mentioned in the present application describe some methods or systems based on a series of steps or devices. However, the present application is not limited to the order of the above steps, that is, the steps can be performed in the order mentioned in the embodiments, or in an order different from the embodiments, or several steps can be performed simultaneously.
[0141] The computer program instructions can also be loaded onto a computer, other programmable data processing apparatus, or other processing device to cause a series of operational steps to be performed on the computer, other programmable apparatus or other processing device to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide processes for implementing the functions / acts specified in the flowchart and / or block diagram block or blocks.
[0142] The above solely describes specific implementations of the present application. For the purpose of description and brevity, the specific working process of the system, module and unit described above can refer to the corresponding process in the foregoing method embodiments, which will not be described herein. It should be understood that the protection scope of the present application is not limited to this. Any person skilled in the art can easily think of various equivalent modifications or replacements within the technical range disclosed in the present application, and these modifications or replacements shall be covered within the protection scope of the present application.
Claims
1. A method of data simulation based on a front-end code file, characterized by, The method comprises the following steps: In the case of obtaining the front-end code file of the http page, based on the user input, add the simulation annotation data corresponding to the page simulation event in the code annotation of the front-end code file, wherein the simulation annotation data includes the request path, request parameter, request response status code, request response body and simulation switch field of the simulation interface, and the simulation switch field is used to represent the simulation interface in the simulation open state or simulation closed state; Generate the request identifier based on the request path and request parameter in the simulation annotation data, and generate the simulation data based on the request response status code, request response body and simulation switch field; Based on the request identifier and simulation data, generate the simulation configuration data corresponding to the page simulation event, and write the simulation configuration data into the simulation configuration file, wherein the simulation configuration file contains the simulation configuration data corresponding to each page simulation event of the http page; In the case of listening to the http request of the http page, intercept the http request, and generate the target request identifier based on the request path and request parameter in the http request; Based on the target request identifier, query the target simulation configuration data matching the target request identifier from the simulation configuration file, only in the case that the simulation switch field in the target simulation configuration data is used to represent the simulation open state, return the simulation data in the target simulation configuration data as the response result, otherwise obtain the real response data by sending the http request to the back-end server as the response result; Wherein, the simulation annotation data further includes the data merging switch field corresponding to the simulation interface, the data merging switch field is used to represent the merging open state or merging closed state, the simulation data is generated based on the request response status code, request response body, simulation switch field and data merging switch field, and the simulation data in the target simulation configuration data is returned as the response result, which comprises: In the case of querying the target simulation configuration data, parse the data merging switch field in the target simulation configuration data; In the case that the data merging switch field is used to represent the merging closed state, only the simulation data in the target simulation configuration data is returned as the response result; In the case that the data merging switch field is used to represent the merging open state, the real response data is obtained by sending the http request to the back-end server, and the real response data and the simulation data are merged as the response result.
2. The method of claim 1, wherein, After generating the simulation configuration data corresponding to the page simulation event, further comprising: Embed the simulation configuration data into the preset code template to obtain the execution code; Add the execution code in the front-end code file to access the simulation configuration data in the global scope based on the execution code.
3. The method of claim 1, wherein, Generating the request identifier based on the request path and request parameter in the simulation annotation data comprises: Perform string segmentation processing on the field value of the request parameter to obtain a plurality of strings; Encrypt the field value of the request path and the plurality of strings by using the digest algorithm to obtain the request identifier.
4. The method of claim 1, wherein, Adding simulation annotation data corresponding to the page simulation event in the code annotation of the front-end code file, including: In the case that the first interface is in an iterative state, determining that the first interface is a simulation interface, defining the request path, request parameter, request response status code and request response body of the first interface in the simulation annotation data, and setting the data merging switch field of the first interface to a merging open state; Wherein, the request response body only contains the newly added response field of the first interface, and the original response field of the first interface is returned through real response data.
5. The method of claim 1, wherein, Merging the real response data and the simulation data to return as a response result, including: Reserving all fields in the simulation data; If a first field in the real response data does not exist in the simulation data, adding the first field in the response result; If a second field exists in both the simulation data and the real response data, deleting the second field in the real response data in the response result.
6. The method of claim 1, wherein, Adding simulation annotation data corresponding to the page simulation event in the code annotation of the front-end code file, including: In the case that the second interface is not online, determining that the second interface is a simulation interface, defining the request path, request parameter, request response status code and request response body of the second interface in the simulation annotation data, and setting the simulation switch field of the second interface to a simulation open state; After adding the simulation annotation data corresponding to the page simulation event in the code annotation of the front-end code file, further including: In the case that the second interface is in an online state, changing the simulation switch field of the second interface in the simulation annotation data to a simulation closed state; In the case that the second interface is in an update-to-be state, modifying the request path, request parameter, request response status code and request response body of the second interface in the simulation annotation data, and re-changing the simulation switch field of the second interface to a simulation open state.
7. A data simulation apparatus based on a front-end code file, characterized by, Including: An adding module is configured to, in the case of obtaining a front-end code file of an http page, add simulation annotation data corresponding to a page simulation event in the code annotation of the front-end code file based on user input, wherein the simulation annotation data includes the request path, request parameter, request response status code, request response body and simulation switch field of a simulation interface, and the simulation switch field is used to represent that the simulation interface is in a simulation open state or a simulation closed state; A generating module is configured to generate a request identifier in combination with the request path and request parameter in the simulation annotation data, and generate simulation data in combination with the request response status code, request response body and simulation switch field; A writing module is configured to generate simulation configuration data corresponding to the page simulation event based on the request identifier and simulation data, and write the simulation configuration data into a simulation configuration file, wherein the simulation configuration file contains simulation configuration data corresponding to each page simulation event of an http page. The interception generation module is configured to, in response to listening to an HTTP request of the HTTP page, intercept the HTTP request, and generate a target request identifier based on a request path and request parameters in the HTTP request; The query module is configured to query, based on the target request identifier, target simulation configuration data matching the target request identifier from the simulation configuration file, return simulation data in the target simulation configuration data as a response result only in a case where a simulation switch field in the target simulation configuration data is used to represent a simulation open state, and otherwise acquire real response data by sending an HTTP request to a backend server as a response result. The simulation annotation data further includes a data merging switch field corresponding to a simulation interface, the data merging switch field being used to represent a merging open state or a merging closed state, and the query module includes: an analysis unit configured to, in response to querying the target simulation configuration data, analyze the data merging switch field in the target simulation configuration data; and a return unit configured to, in a case where the data merging switch field is used to represent the merging closed state, return only the simulation data in the target simulation configuration data as the response result, and in a case where the data merging switch field is used to represent the merging open state, acquire real response data by sending an HTTP request to the backend server, and return the real response data and the simulation data as the response result after merging.
8. An electronic device, comprising: The electronic device includes a processor and a memory storing computer program instructions, and the electronic device implements the method according to any one of claims 1-6 when executing the computer program instructions.
9. A computer-readable storage medium, characterized in that, The computer readable storage medium stores computer program instructions, and the computer program instructions are executed by the processor to implement the method according to any one of claims 1-6.
Citation Information
Patent Citations
Text generation method and device, electronic equipment, storage medium and product
CN114528013A
Interface simulation method and device, storage medium and electronic equipment
CN115016775A