Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.github.kristofa.test.http.file;
  
  import java.io.File;
  
com.github.kristofa.test.http.HttpResponseProvider that is able to serve responses for requests/responses previously saved by HttpRequestResponseFileLogger.

It supports submitting same request multiple times with different return result.

It reads all http requests on construction and keeps them in memory. It does not keep responses in memory. They are read from disk on request and not cached.

 
 
     private final String directory;
     private final String fileName;
     private final HttpResponseFileReader httpResponseFileReader;

    
Creates a new instance. Will try to find request/response files and will throw unchecked exception in case:
  • We can not find at least 1 request/response for given directory and file name.
  • We found a request without persisted response

Parameters:
directory Directory from which to read files.
fileName Base file name. Should not contain extension. Will be suffixed with sequence number and .txt extension. Should be same as used in HttpRequestResponseFileLogger.
 
     public FileHttpResponseProvider(final String directoryfinal String fileName) {
         this(directoryfileNamenew HttpRequestFileReaderImpl(), new HttpResponseFileReaderImpl());
     }

    
Creates a new instance. Will try to find request/response files and will throw unchecked exception in case:
  • We can not find at least 1 request/response for given directory and file name.
  • We found a request without persisted response

Parameters:
directory Directory from which to read files.
fileName Base file name. Should not contain extension. Will be suffixed with sequence number and .txt extension. Should be same as used in HttpRequestResponseFileLogger.
requestFileReader HTTP request file reader.
responseFileReader HTTP response file reader.
 
     public FileHttpResponseProvider(final String directoryfinal String fileName,
         final HttpRequestFileReader requestFileReaderfinal HttpResponseFileReader responseFileReader) {
         this. = directory;
         this. = fileName;
          = requestFileReader;
          = responseFileReader;
     }

    
 
     @Override
     protected void lazyInitializeExpectedRequestsAndResponses() {
         int seqNr = 1;
 
         File requestFile = new File(..getFileName(seqNr));
         if (!requestFile.exists()) {
             throw new IllegalStateException("No saved http request/responses found. File " + requestFile + " not found.");
         }
 
         while (requestFile.exists()) {
             final File responseFile = new File(..getFileName(seqNr));
             if (!responseFile.exists()) {
                 throw new IllegalStateException("Found request file (" + requestFile + ") but no matching response file: "
                     + responseFile);
             }
             submitRequest(seqNr);
             seqNr++;
             requestFile = new File(..getFileName(seqNr));
         }
     }
 
     private void submitRequest(final String fileNamefinal int seqNr) {
 
         final File requestFile = new File(..getFileName(fileNameseqNr));
         final File requestEntityFile =
             new File(..getFileName(fileNameseqNr));
         final HttpRequest request = .read(requestFilerequestEntityFile);
         final FileHttpResponseProxy responseProxy =
             new FileHttpResponseProxy(fileNameseqNr);
         addExpected(requestresponseProxy);
     }
New to GrepCode? Check out our FAQ X